HOG2
Prim.cpp
Go to the documentation of this file.
1 //
2 // Prim.cpp
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 8/9/17.
6 // Copyright © 2017 University of Denver. All rights reserved.
7 //
8 
9 #include <queue>
10 #include "Prim.h"
11 
12 class Compare
13 {
14 public:
15  bool operator() (edge *e1, edge *e2)
16  {
17  return e1->GetWeight() > e2->GetWeight();
18  }
19 };
20 
21 void Prim(Graph *g)
22 {
23  std::priority_queue<edge *, std::vector<edge *>, Compare> q;
24 
25  for (int x = 0; x < g->GetNumEdges(); x++)
26  {
27  edge *e = g->GetEdge(x);
28  e->setMarked(false);
29  }
30  for (int x = 0; x < g->GetNumNodes(); x++)
31  {
32  node *n = g->GetNode(x);
33  n->SetKeyLabel(0);
34  }
35  node *n = g->GetNode(0);
36  n->SetKeyLabel(1);
37  for (int x = 0; x < n->GetNumEdges(); x++)
38  {
39  edge *e = n->getEdge(x);
40  q.push(e);
41  }
42  while (q.size() > 0)
43  {
44  edge *e = q.top();
45  q.pop();
46  if (e->getMarked())
47  continue;
48  if (g->GetNode(e->getFrom())->GetKeyLabel() == 0 ||
49  g->GetNode(e->getTo())->GetKeyLabel() == 0)
50  {
51 // printf("*Adding %d-%d cost %d to MST\n", e->getFrom(), e->getTo(), (int)e->GetWeight());
52  e->setMarked(true);
53  }
54 
55  node *n = g->GetNode(e->getFrom());
56  if (n->GetKeyLabel() == 1)
57  {
58  n = g->GetNode(e->getTo());
59  if (n->GetKeyLabel() == 1)
60  continue;
61  }
62 // printf("*Processing %d\n", n->GetNum());
63  n->SetKeyLabel(1);
64  for (int x = 0; x < n->GetNumEdges(); x++)
65  {
66  edge *e = n->getEdge(x);
67  if (e->getMarked() == true)
68  continue;
69  if (e->getFrom() == n->GetNum())
70  {
71  if (g->GetNode(e->getTo())->GetKeyLabel() == 0)
72  {
73  q.push(e);
74 // printf("--Adding edge (%d %d)\n", e->getFrom(), e->getTo());
75  }
76  }
77  else {
78  if (g->GetNode(e->getFrom())->GetKeyLabel() == 0)
79  {
80  q.push(e);
81 // printf("--Adding edge (%d %d)\n", e->getTo(), e->getFrom());
82  }
83  }
84  }
85  }
86 }
edge::GetWeight
double GetWeight()
Definition: Graph.h:140
edge::getFrom
unsigned int getFrom() const
Definition: Graph.h:146
edge::getTo
unsigned int getTo() const
Definition: Graph.h:147
edge::getMarked
bool getMarked()
Definition: Graph.h:150
Prim.h
Compare::operator()
bool operator()(edge *e1, edge *e2)
Definition: Prim.cpp:15
edge::setMarked
void setMarked(bool marked)
Definition: Graph.h:149
Graph
A generic Graph class.
Definition: Graph.h:66
Graph::GetNode
node * GetNode(unsigned long num)
Definition: Graph.cpp:152
node::getEdge
edge * getEdge(unsigned int which)
Definition: Graph.cpp:794
node::GetKeyLabel
int GetKeyLabel()
Definition: Graph.h:209
node::SetKeyLabel
void SetKeyLabel(int which)
Definition: Graph.h:208
Graph::GetNumEdges
int GetNumEdges()
Definition: Graph.cpp:397
Graph::GetNumNodes
int GetNumNodes()
Definition: Graph.cpp:403
node::GetNum
unsigned int GetNum() const
Definition: Graph.h:176
Compare
Definition: Prim.cpp:12
node::GetNumEdges
int GetNumEdges()
Definition: Graph.h:204
node
Nodes to be stored within a Graph.
Definition: Graph.h:170
Prim
void Prim(Graph *g)
Definition: Prim.cpp:21
Graph::GetEdge
edge * GetEdge(unsigned long num)
Definition: Graph.cpp:164
edge
Edge class for connections between node in a Graph.
Definition: Graph.h:129