View Single Post
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

Graph help please

 
0
  #1
Nov 11th, 2008
This grap is kicking my butt. I am not sure how to do it. Can someone please help explain these to me.

  1. // graph.h
  2. // -- adjacency list representation of a weighted graph
  3. //
  4.  
  5. #ifndef GRAPH_H
  6.  
  7. #define GRAPH_H
  8.  
  9. #include <string>
  10. #include "queue.h"
  11. using namespace std;
  12.  
  13.  
  14. class FileOpenError // Exception class -- cannot open input file
  15. {
  16. };
  17.  
  18. struct EdgeNode // Structure representing an edge
  19. {
  20. int destination; // Index of destination vertex
  21. int weight; // Edge weight
  22. EdgeNode* nextPtr; // Next edge pointer
  23. };
  24.  
  25. struct VertexNode // Structure representing a vertex
  26. {
  27. string vname; // Name of vertex
  28. bool mark; // Marked flag
  29. EdgeNode* edgePtr; // Pointer to list of edges
  30. };
  31.  
  32.  
  33. class Graph // Graph ADT using adjacency list representation
  34. {
  35. private: //***** Private class members below *****//
  36. VertexNode* vertices; // Array of vertex nodes
  37. int numV; // Number of vertices
  38.  
  39. public: //***** Public members below *****//
  40. Graph(int num); // Constructor - creates graph with num vertices
  41. ~Graph(); // Destructor - deallocates all edge nodes and the vertex list
  42. void DeleteAllEdges(); // Deallocates all edge nodes from all vertices
  43. bool IsEmpty(); // Returns true if graph empty, false otherwise
  44. bool IsFull(); // Returns true if graph full, false otherwise
  45. void AddVertex(string v); // Adds vertex to graph assuming vertex not already present
  46. void AddEdge(string s, string d, int w); // Adds edge from source S to destination D with specified weight W
  47. int IndexIs(string v); // Returns index of edge from S to D; -1 if not present
  48. int WeightIs(string s, string d); // Returns weight of vertex V -- assumes V is in graph
  49. void GetToVertices(string s, Queue& q); // Returns a queue Q of vertices adjacent to vertex V
  50. void ClearMarks(); // Clears vertex marks
  51. void MarkVertex(string v); // Marks vertex V
  52. bool IsMarked(string v); // Returns true if vertex V is marked, false otherwise
  53. void Print(); // Write graph to stdout
  54. Queue* DepthFirstSearch(string startVertex, string endVertex); // Returns ptr to queue containing path or NULL if none
  55. };
  56.  
  57. void Load(string filename, Graph*& g, string& startVertex, string& endVertex);
  58. // Load graph from named file and values of start and end vertices
  59. // Note: this file attempts to open the named file for input and input all information.
  60. // If the file fails to open, Load throws the FileOpenError exception
  61.  
  62. #endif

This is what I have so far but I don't think this is right. Can someone please explain these functions I just don't get it.

  1. #include <iostream>
  2. #include <new>
  3. #include <cstddef>
  4. #include <string>
  5. #include "graph.h"
  6.  
  7. using namespace std;
  8.  
  9. Graph::Graph(int num)
  10. {
  11. numV = 0;
  12. vertices[num];
  13. }
  14.  
  15. Graph::~Graph()
  16. {
  17. delete [] vertices;
  18. }
  19.  
  20. bool Graph::IsEmpty()
  21. {
  22. return (numV == 0);
  23. }
  24.  
  25. bool Graph::IsFull()
  26. {
  27. EdgeNode* tempPtr;
  28. try
  29. {
  30. tempPtr = new EdgeNode;
  31. }
  32. catch (bad_alloc)
  33. {
  34. return true;
  35. }
  36. delete tempPtr;
  37. return false;
  38. }
  39.  
  40. void Graph::AddVertex(string v)
  41. {
  42. verticies[numV] = v;
  43.  
  44. for(int index = 0; index < numV; index++)
  45. {
  46.  
  47. }
Reply With Quote