A little graph help please marking a visited node

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2008
Posts: 31
Reputation: JustLearning is an unknown quantity at this point 
Solved Threads: 0
JustLearning JustLearning is offline Offline
Light Poster

A little graph help please marking a visited node

 
0
  #1
Nov 14th, 2008
The function that I am having a problem with is in the middle of this seperated from the rest of the code.
  1. #ifndef GRAPH_H
  2.  
  3. #define GRAPH_H
  4.  
  5. #include <string>
  6. #include "queue.h"
  7. using namespace std;
  8.  
  9.  
  10. class FileOpenError // Exception class -- cannot open input file
  11. {
  12. };
  13.  
  14. struct EdgeNode // Structure representing an edge
  15. {
  16. int destination; // Index of destination vertex
  17. int weight; // Edge weight
  18. EdgeNode* nextPtr; // Next edge pointer
  19. };
  20.  
  21. struct VertexNode // Structure representing a vertex
  22. {
  23. string vname; // Name of vertex
  24. bool mark; // Marked flag
  25. EdgeNode* edgePtr; // Pointer to list of edges
  26. };
  27.  
  28.  
  29. class Graph // Graph ADT using adjacency list representation
  30. {
  31. private: //***** Private class members below *****//
  32. VertexNode* vertices; // Array of vertex nodes
  33. int numV; // Number of vertices
  34.  
  35. public: //***** Public members below *****//
  36. Graph(int num); // Constructor - creates graph with num vertices
  37. ~Graph(); // Destructor - deallocates all edge nodes and the vertex list
  38. void DeleteAllEdges(); // Deallocates all edge nodes from all vertices
  39. bool IsEmpty(); // Returns true if graph empty, false otherwise
  40. bool IsFull(); // Returns true if graph full, false otherwise
  41. void AddVertex(string v); // Adds vertex to graph assuming vertex not already present
  42. void AddEdge(string s, string d, int w); // Adds edge from source S to destination D with specified weight W
  43. int IndexIs(string v); // Returns index of edge from S to D; -1 if not present
  44. int WeightIs(string s, string d); // Returns weight of vertex V -- assumes V is in graph
  45. void GetToVertices(string s, Queue& q); // Returns a queue Q of vertices adjacent to vertex V
  46. void ClearMarks(); // Clears vertex marks
  47. void MarkVertex(string v); // Marks vertex V
  48. bool IsMarked(string v); // Returns true if vertex V is marked, false otherwise
  49. void Print(); // Write graph to stdout
  50. Queue* DepthFirstSearch(string startVertex, string endVertex); // Returns ptr to queue containing path or NULL if none
  51. };
  52.  
  53. void Load(string filename, Graph*& g, string& startVertex, string& endVertex);
  54. // Load graph from named file and values of start and end vertices
  55. // Note: this file attempts to open the named file for input and input all information.
  56. // If the file fails to open, Load throws the FileOpenError exception
  57.  
  58. #endif
  59.  
  60. using namespace std;
  61.  
  62. Graph::Graph(int num):vertices(NULL),numV(num)
  63. {
  64. if (num) {
  65. vertices = new VertexNode[num];
  66. }
  67. }
  68.  
  69. Graph::~Graph()
  70. {
  71. delete [] vertices;
  72. }
  73.  
  74. void Graph::DeleteAllEdges()
  75. {
  76. VertexNode* temp = NULL;
  77. }
  78.  
  79. bool Graph::IsEmpty()
  80. {
  81. return(numV == 0);
  82. }
  83.  
  84. bool Graph::IsFull()
  85. {
  86.  
  87. }
  88.  
  89. void Graph::AddVertex(string v)
  90. {
  91. for(int numNodes = 0; numNodes <= numV; numNodes++)
  92. {
  93. if (vertices[numNodes].vname.empty()) {
  94. vertices[numNodes].vname = v;
  95. vertices[numNodes].edgePtr = NULL;
  96. vertices[numNodes].mark = false;
  97. break;
  98. }
  99. //Graph::AddVertex(string v);
  100. }
  101. }
  102.  
  103. void Graph::AddEdge(string s, string d, int w)
  104. {
  105. int sV = IndexIs(s);
  106. int dV = IndexIs(d);
  107.  
  108. EdgeNode *edge = vertices[sV].edgePtr;
  109. vertices[sV].edgePtr = new EdgeNode;
  110. vertices[sV].edgePtr->nextPtr = edge;
  111. vertices[sV].edgePtr->weight = w;
  112. vertices[sV].edgePtr->destination = dV;
  113.  
  114. }
  115.  
  116. int Graph::IndexIs(string v)
  117. {
  118. for(int numNodes = 0; numNodes <= numV; numNodes++)
  119. {
  120. if (vertices[numNodes].vname == v) {
  121. return numNodes;
  122. }
  123. }
  124. return -1;
  125. }
  126.  
  127. int Graph::WeightIs(string s, string d)
  128. {
  129. int sV = IndexIs(s);
  130. int dV = IndexIs(d);
  131.  
  132. EdgeNode *edge = vertices[sV].edgePtr;
  133. while (edge) {
  134. if (edge->destination == dV) {
  135. return edge->weight;
  136. }
  137. edge = edge->nextPtr;
  138. }
  139. return -1;
  140. }
  141.  
  142. void Graph::GetToVertices(string s, Queue& q)
  143. {
  144. int sV = IndexIs(s);
  145.  
  146. EdgeNode *edge = vertices[sV].edgePtr;
  147. while (edge) {
  148. q.Enqueue(s);
  149. edge = edge->nextPtr;
  150. }
  151. }
  152.  
  153. void Graph::ClearMarks()
  154. {
  155. }

The problem function
How do I mark the node as visited?


  1. void Graph::MarkVertex(string v)
  2. {
  3. bool marked = v;
  4. }


  1. bool Graph::IsMarked(string v)
  2. {
  3. }
  4.  
  5. void Graph::Print()
  6. {
  7.  
  8. for (int node = 0; node <+ numV; node++){
  9. cout << vertices[node].vname;
  10. if (!vertices[node].edgePtr){
  11. cout << endl;
  12. continue;
  13.  
  14. }
  15. EdgeNode *edge = vertices[node].edgePtr;
  16. while (edge) {
  17. cout << vertices[edge->destination].vname << edge->weight;
  18. edge = edge->nextPtr;
  19. }
  20. cout << endl;
  21. }
  22.  
  23.  
  24. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,739
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 281
Lerner Lerner is offline Offline
Posting Virtuoso

Re: A little graph help please marking a visited node

 
0
  #2
Nov 15th, 2008
  1. void Graph::MarkVertex(string v)
  2. {
  3. if(v == vname)
  4. marked = true;
  5. }
Last edited by Lerner; Nov 15th, 2008 at 12:18 pm.
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC