Kruskal's algorithm from Sedgewick

Jul 1, 2008 at 11:50pm
I want to implement kruskal's algorithm on C++ for my program, but my knowledge of templates and vector is weak. I don't know where to start and I'm time-pressed for my project.This is kruskal's pseudocode from Sedgewick's


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
program kruskal (input)

const maxV = 50, maxE=2500

type edge = record x,y weight: integer end;
var i,j,m,x,y,V,E:integer;
     edges: array [0..maxE] of edge;
begin

readln(V,E);
for j:=1 to E do
    begin
    readln (c,d, edges[j].weight);
    edges[j].x:=index(c);
    edges[j].y :=index(d);
end;

findinit; pqconstruct; i:=0; // what is findinit? pqconstruct?

repeat
m:= pqremove; x:=edges[m].x; y:=edges[m].y; // what is pqremove?
if not fastfind(x,y,true) then  // what is fastfind? how do I implement it?
   begin
   writeln(name(x),name(y), edges[m].weight);
   i:=i+1;
   end
until i=V-1
end.


How do I implement those functions (findinit, pqconstruct, pqremove, fastfind) on C++?

Also:
I found this code from Sedgewick's other book on Algorithms and Data Structures (this time already implemented on C++) And when trying to compile this piece of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
class Edge 
{ 
    public:
      Edge(int, int, double);
      int v() const;
      int w() const;
      double wt() const;
      bool from(int) const;
      int other(int) const;
  };
template <class Edge> class Graph
  {
    public:
      Graph(int, bool);
      ~Graph();
      int V() const;
      int E() const;
      bool directed() const;
      int insert(Edge *);
      int remove(Edge *);
      Edge *edge(int, int);
      class adjIterator
        {
          public:
            adjIterator(const Graph &, int);
            Edge *beg();
            Edge *nxt();
            bool end();
        };
  };


template <class Graph, Edge> // gcc gives compilation error here, why?
vector <Edge *> edges(const Graph &G)
{ int E = 0;
  vector <Edge *> a(G.E()); 
  for (int v = 0; v < G.V(); v++) 
    {
      typename Graph::adjIterator A(G, v);
      for (Edge* e = A.beg(); !A.end(); e = A.nxt()) 
        if (e->from(v)) a[E++] = e;
    }
  return a;
}

int main (){

	return 0;
}


gcc gives the following error:

sedgewick.cpp:33: error: expected constructor, destructor, or type conversion before ‘<’ token

Why? How can I fix it?
Last edited on Jul 2, 2008 at 12:09am
Jul 2, 2008 at 2:10am
Because line 33 should read
template <class Graph, class Edge>

Hope this helps.
Jul 2, 2008 at 4:58am
No, it's still not working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Edge 
{ 
    public:
      Edge(int, int, double);
      int v() const;
      int w() const;
      double wt() const;
      bool from(int) const;
      int other(int) const;
  };
template <class Edge> class Graph
  {
    public:
      Graph(int, bool);
      ~Graph();
      int V() const;
      int E() const;
      bool directed() const;
      int insert(Edge *);
      int remove(Edge *);
      Edge *edge(int, int);
      class adjIterator
        {
          public:
            adjIterator(const Graph &, int);
            Edge *beg();
            Edge *nxt();
            bool end();
        }
  }


template <class Graph, class Edge> // gcc gives compilation error here, why?
vector <Edge *> edges(const Graph &G)
{ int E = 0;
  vector <Edge *> a(G.E()); 
  for (int v = 0; v < G.V(); v++) 
    {
      typename Graph::adjIterator A(G, v);
      for (Edge* e = A.beg(); !A.end(); e = A.nxt()) 
        if (e->from(v)) a[E++] = e;
    }
  return a;
}

int main (){

	return 0;
}


gcc says the same thing:

sedgewick.cpp:36: error: expected constructor, destructor, or type conversion before ‘<’ token

Topic archived. No new replies allowed.