Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Graph Theory: Finding Strongly Connected Components in Directed Graphs, Assignments of Computer Science

Instructions for a programming assignment in which students are required to build a graph module in c, implement depth first search (dfs), and use the graph module to find the strongly connected components of a directed graph. The concept of strongly connected components and provides an example of how to find them using dfs and the transpose of a graph.

Typology: Assignments

Pre 2010

Uploaded on 08/19/2009

koofers-user-lt7-1
koofers-user-lt7-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

Related documents


Partial preview of the text

Download Graph Theory: Finding Strongly Connected Components in Directed Graphs and more Assignments Computer Science in PDF only on Docsity!

CMPS 101

Abstract Data Types Spring 2003

Programming Assignment 4

Due Wednesday May 21, 12:00 midnight

In this assignment you will build a Graph module in C, implement Depth First Search (DFS), and use your Graph module to find the strongly connected components of a directed graph. Begin by reading sections 22.3-22.5 in the text. A directed graph (or digraph) G^ (^ V^ , E ) is said to be strongly connected if for every pair of vertices u , vV , u is reachable from v and v is reachable from u. Most directed graphs are not strongly connected. In general we say a subset X^ ^ V is strongly connected if every vertex in X is reachable from every other vertex in X. A subset which is maximal with respect to this property is called a strongly connected component of G. In other words, X^ ^ V is a strongly connected component of G if and only if X is strongly connected, and the addition of one more vertex to X would create a subset which is not strongly connected. Example 1 2 3 4 G 5 6 7 8 It’s easy to see that there are 4 strong components of G : C 1^ {^1 ,^2 ,^5 }, C^ 2 {^3 ,^4 }, C^ 3 {^6 ,^7 }, and C 4 (^) { 8 }. To find the strong components of a digraph G first call DFS(^ G^ ), and as vertices are finished, place them on a stack. When this is complete the stack stores the vertices sorted by decreasing finish times. Next compute the transpose G T of G , which is obtained by reversing the directions on all edges of G. Finally, run DFS(^^ G^ T ), but in the main loop (lines 5-7) process vertices by decreasing finish times. (Here we mean finish times from the first call to DFS.) This is accomplished by simply taking vertices off the stack in loop 5-7 of DFS. When this process is complete the vertices of each tree in the resulting DFS forest constitute the strong components of G. (Note the strong components of G are identical to the strong components of G T .) See the algorithm and proof of correctness in section 22.5 of the text. In this assignment you will create a graph module in C which represents a directed graph by an array of adjacency lists. Your graph module will, among other things, provide the capability of running DFS, and computing the transpose of a directed graph. Here is a catalog of required functions and their prototypes: /* Constructors-Destructors */ GraphHndl NewGraph(int n); void FreeGraph(GraphHndl G); / Access functions / int getOrder(GraphHndl G); int getParent(GraphHndl G, int u); / Pre: 1<=u<=n=getOrder(G) / int getDiscover(GraphHndl G, int u); / Pre: 1<=u<=n=getOrder(G) */

int getFinish(GraphHndl G, int u); /* Pre: 1<=u<=n=getOrder(G) / / Manipulation procedures / void addDirectedEdge(GraphHndl G, int u, int v); / Pre: 1<=u<=n, 1<=v<=n / void DFS(GraphHndl G, ListHndl S); / Other Functions / GraphHndl Transpose(GraphHndl G); GraphHndl copyGraph(GraphHndl G); void printGraph(GraphHndl G, FILE out); Function NewGraph will return a handle to a new graph object containing n vertices and no edges. FreeGraph frees all heap memory associated with a graph object and sets it’s GraphHndl argument to NULL. Function getOrder returns the number of vertices in the graph G , while functions getParent, getDiscover, and getFinish return the appropriate field values for the given vertex. Note that the parent of a vertex may be NIL. Also the discover and finish times of vertices will be undefined before DFS is called. It is recommended that you #define constant macros for NIL and UNDEF which will represent those values. The function addDirectedEdge will add vertex v to the adjacency list of vertex u (but not u to the adjacency list of v ), thus establishing a directed edge from u to v. The function DFS will perform the depth first search algorithm on G. The List S has two purposes in this function. First it defines the order in which vertices will be processed in the main loop (5-7) of DFS. Second, when DFS is complete, it will store the vertices in order of decreasing finish times (hence S can be considered to be a stack). The List S can therefore be classified as both an input and an output parameter to the function. Obviously you should utilize the List module you created in pa2 to implement S and the adjacency lists which represent G. DFS has two preconditions: getLength( S ) = n , and S contains some permutation of the integers {1, 2, ..., n } where n = getOrder(G). You are required to check the first condition but not the second. Recall that DFS calls the recursive algorithm Visit (DFS-Visit in the text), and uses a global variable called time. There are two possible approaches to implementing Visit. You can define Visit as a top level function in your graph implementation file (which is of course not exported), and let time be a static variable whose scope is the entire file. This approach is not recommended since other functions would have access to the global time variable. The other approach (which is recommended) is to let time be a local variable in DFS, and place the definition of Visit within the definition of DFS. Since time is local to DFS, it’s scope includes the defining block for Visit, and is static throughout all recursive calls to Visit. This may be tricky if you’re not used to nesting function definitions since there are issues of scope to deal with. Try experimenting with a few simple examples to make sure you know how it works. The first approach is still a valid option if you prefer. Note that although nesting function definitions is not a standard feature of ANSI C, and is not supported by many compilers, it is supported by the gcc compiler. If you plan to develop your project on another platform, it may be necessary to use the first approach. Function Transpose returns a handle to a new graph object representing the transpose of G , and copyGraph returns a handle to a new graph which is a copy of G. Both Transpose and copyGraph could be considered constructors since they create new graph objects. Function printGraph prints the adjacency list representation of G to the file pointed to by out. The client of your Graph module will be called FindComponents. It will take two command line arguments giving the names of the input and output files respectively: % FindComponents infile outfile The main program FindComponents will do the following:

 Read the input file.  Assemble a graph object G using NewGraph and addDirectedEdge.  Print the adjacency list representation G to the output file.  Run DFS on G and G T , processing the vertices in the second call by decreasing finish times from the first call.  Determine the strong components of G.  Print the strong components of G to the output file. Input and output file formats are illustrated in the following example, which corresponds to the directed graph on the first page of this handout: Input: 8 1 2 2 3 2 5 2 6 3 4 3 7 4 3 4 8 5 1 5 6 6 7 7 6 7 8 8 8 0 0 Output: Adjacency list representation: 1: 2 2: 3 5 6 3: 4 7 4: 3 8 5: 1 6 6: 7 7: 6 8 8: 8 There are 4 strongly connected components: Component 1: 1 5 2 Component 2: 3 4 Component 3: 7 6 Component 4: 8 Observe that the input file format is very similar to that of pa3. The first line gives the number of vertices in the graph, subsequent lines specify directed edges, and input is terminated by the ‘dummy’ line 0 0. You have basically the same two design options for your Graph module that you did in pa3. On the one hand a the struct GraphStruct may contain array fields for the parent, discover, finish, and color attributes, as well as an array of ListHndls for the adjacency lists. For each array, index i then pertains to the vertex with label i. (Again I recommend that arrays be built to begin indexing at 1, i.e. just waste the zeroth element.) The second option would be to define a private inner struct called Vertex (analogous to Node in

List) which encapsulates several attributes, such as parent, discover, finish, and color. Then GraphStruct would contain arrays of Vertex and ListHndl variables. Just as before, either option is acceptable. You are required to submit the following files: README, Makefile, List.c, List.h, ListDriver.c, Graph.h, Graph.c, GraphDriver.c, FindComponents.c. As usual README contains a catalog of submitted files and any special notes to the grader. Makefile should be capable of making the executables ListDriver, GraphDriver, FindComponents, and should contain a clean utility which removes all object files. The grader will be taking a second look at your List module so be sure to fix any errors he pointed out in pa2. Graph.h and Graph.h are the implementation and interface files for your Graph module. GraphDriver.c is used for testing of your Graph module. FindComponents.c implements the top level client and main program for this project. By now everyone knows that points are deducted both for neglecting to include required files, and for submitting additional unwanted files, but let me say it anyway: do not submit binary files of any kind. Note that FindComponents needs to pass a List to the function DFS, so it is also a client of the List module and must therefore #include the file List.h. Also note that the Graph module will be exporting a function which takes a ListHndl argument (namely DFS). Therefore not only must Graph.c #include List.h, but Graph.h must do so as well. Below is a Makefile which you may use or alter as you see fit:

Makefile for Graph ADT and related modules.

make makes FindComponents

make GraphDriver makes GraphDriver

make ListDriver makes ListDriver

make clean removes all object and executable files

FindComponents : FindComponents.o Graph.o List.o gcc -o FindComponents FindComponents.o Graph.o List.o GraphDriver : GraphDriver.o Graph.o List.o gcc -o GraphDriver GraphDriver.o Graph.o List.o ListDriver : ListDriver.o List.o gcc -o ListDriver ListDriver.o List.o FindComponents.o : FindComponents.c Graph.h List.h gcc -c -ansi -Wall FindComponents.c GraphDriver.o : GraphDriver.c Graph.h List.h gcc -c -ansi -Wall GraphDriver.c ListDriver.o : ListDriver.c List.h gcc -c -ansi -Wall ListDriver.c Graph.o : Graph.c Graph.h List.h gcc -c -ansi -Wall Graph.c List.o : List.c List.h gcc -c -ansi -Wall List.c clean : rm FindComponents GraphDriver ListDriver FindComponents.o
GraphDriver.o ListDriver.o Graph.o List.o