CISC4080, Computer Algorithms,
Lab 6 (Graph, part 2)

     

Home

Schedule

Assignments

Grading

 

Goal

We will study:

  1. Practice implementing two graph search algorithms: depth-first, and breadth-first.
  2. Practice using graph search algorithms to solve other graph problems.

Using C++ STL classes

  1. The sample graph class uses vector and list. They are container classes implemented using dynamic array and linked list as learned in data structure. Here are some beginners guide if you need more explanation and example other than the documents (yolinux tutorials for C++ STL library).
  2. You should also use queue while implementing BFS.
  3. Here is an example code demonstrating both reading from a text file and using STL map class:map_example.cpp.
    cp ~zhang/public_html/cs4080/Demo/map_example.cpp . 
    cp ~zhang/public_html/cs4080/Demo/article.txt . 
    

Graph Representation

You are welcome to use the following code to start your own lab with, or to write everything from ground up. Use the following command to copy the files over:

cp ~zhang/public_html/cs4080/Demo/Graph*.* . 
The above command will copy the following files to your current directory( Graph.h, Graph.cpp, Graph_driver.cpp). Note that for template class (or class template), you need to include the implementation file in the driver file.. (Here is a post explainning why that's the case, and another one is here.)
//Implement Graph as a template class -- allow us to use it to create
// graph where the nodes are of different type: string, int, char, 2D array ...
template <class NodeType>
class Graph{
public:
	... 
private:
	bool directed; //true: directed graph, false: undirected graph 

	vector<NodeType> nodes; //vector is a container class like that
	 //"unsorted list implemented using dynamic array" learned in data structure
	 // it overload [] operator, so you can access it like accessing array

	//The following is adjacency lists 
	//    edges[i] is adjacency list for nodes[i], storing
	//   the index of node that node i is connected to 
	//
	//for undirected graph, we store each edge twice, for example,
	// if node a and b are connected by an edge, 
	//   b is in the adjacent list of a, and a is in the adjacent list of b
	vector<list<int>> edges; 

};

Requirements:

  1. Based upon the DFS algorithms discussed in class, implement the following member functions of the template graph class, test them on simple graphs.
    	//Explore and visit all vertices of the graph 
    	// by calling DFS_Visit from a randomly chosen vertex,
    	// and then if there are vertices still not visited, call
    	// DFS_Visit from a randomly chosen unvisited vertex, and so on...
    	// When finish: Display 
    	//         * the predecessor of all vertices (DFS tree/forest)
    	//         * the discover and finish time of each vertices 
            void DFS (); 
    	         
    	//Explore and visit all vertices of the graph that are 
    	//reachable from node s in DFS order 
    	// Whenever a back edge is found, 
    	//	i.e., when you explore neighbors of node u, and find a node v which 
    	//	has been visited already (i.e., not WHITE) and also v is an ancestor of u. 
    	//	(Hint: v is an ancestor of u if and only if v is parent of 
    	//	u, or parent of parent of u, or parent of parent of parent of u)
    	void DFS_Visit (NodeType s,... );
    	                          //^ You will need more parameters... 
    
  2. Test your DFS algorithm on the following two graphs: directed graph test case, undirected graph test case.
  3. At the end of the DFS search, output the parent node for each node, whether the graph has cycle (using back edge detection), and examine the order in which the nodes finish DFS_visit(using the output line added in DFS_visit function)
  4. Implement topological sort algorithm, and test it on the getting-dressed graph as given in the slides. Note that you will need to make DFS return some result to the caller in order to call it in your topological sort function.

Extra Credits

  1. Implement DFS algorithm without recursion (using stack to backtrack).
  2. Extend BFS algorithm to implement Dijkstra's Algorithm
    • Extend Graph class so that each edge has a double valued weight field
      • You can use adjacency matrix, or
      • You can use adjacency list:
        	typedef struct EdgeInfo {
        		int NodeIndex;  //to which node ?
        		double weight;	//cost,distance of the edge
        	};
        
        	class Graph{
        		...
        	        vector<list<EdgeInfo>> edges;
        	};
        		
    • Implement Digistra's shortest-path algorithm (page 110 of textbook)

Submission

Please submit each source files, class specficiation file (header file) and implemenetation file (.cpp file), and driver program using the script given. For example,

submit4080 LAB6 Graph.cpp
...