Algorithms and Abstract Data Types - Programming Assignment 5 | CMPS 101, Assignments of Computer Science

Material Type: Assignment; Class: Algorithms and Abstract Data Types; Subject: Computer Science; University: University of California-Santa Cruz; Term: Unknown 2008;

Typology: Assignments

Pre 2010

Uploaded on 08/19/2009

koofers-user-w8v
koofers-user-w8v 🇺🇸

9 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMPS 101
Algorithms and Abstract Data Types
Summer 2008
Programming Assignment 5
Due Tuesday August 12, 10:00 pm
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 digraph
),( EVG
is said to be strongly connected if for every pair of vertices
Vvu ,
, u is
reachable from v, and v is reachable from u. Most directed graphs are not strongly connected. In general
we say a subset
VX
is strongly connected if every vertex in X is reachable from every other vertex in
X. A strongly connected subset which is maximal with respect to this property is called a strongly
connected component of G. In other words,
VX
is a strongly connected component of G if and only
if (i) X is strongly connected, and (ii) 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:
}5 ,2 ,1{
1
C
,
}4 ,3{
2
C
,
}7 ,6{
3
C
, and
}8{
4
C
.
To find the strong components of a digraph G first call
)(DFS G
, then as vertices are finished, place
them on a stack. When this is complete the stack stores the vertices ordered by decreasing finish times.
Next compute the transpose
of G, which is obtained by reversing the directions on all edges of G.
Finally, run
)(DFS
T
G
, but in the main loop (lines 5-7) of DFS, process vertices by decreasing finish
times. (Here we mean finish times from the first call to DFS.) This is accomplished by simply popping
vertices off the stack created in the first call. When this process is complete the trees in the resulting
Depth First forest constitute the strong components of G. (Note the strong components of G are identical
to the strong components of
.) See the algorithm (Strongly-Connected-Components) 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. DFS requires that vertices posses attributes for color (white,
black, grey), discover time, finish time, and parent. Here is a catalog of required functions and their
prototypes:
/* Constructors-Destructors */
GraphRef newGraph(int n);
void freeGraph(GraphRef* pG);
/* Access functions */
pf3
pf4

Partial preview of the text

Download Algorithms and Abstract Data Types - Programming Assignment 5 | CMPS 101 and more Assignments Computer Science in PDF only on Docsity!

CMPS 101

Algorithms and Abstract Data Types Summer 2008

Programming Assignment 5

Due Tuesday August 12, 10:00 pm

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 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 strongly connected 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 (i) X is strongly connected, and (ii) 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 )^ , then as vertices are finished, place them on a stack. When this is complete the stack stores the vertices ordered 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) of DFS, process vertices by decreasing finish times. (Here we mean finish times from the first call to DFS.) This is accomplished by simply popping vertices off the stack created in the first call. When this process is complete the trees in the resulting Depth First 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 (Strongly-Connected-Components) 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. DFS requires that vertices posses attributes for color (white, black, grey), discover time, finish time, and parent. Here is a catalog of required functions and their prototypes: /* Constructors-Destructors / GraphRef newGraph(int n); void freeGraph(GraphRef pG); /* Access functions */

int getOrder(GraphRef G); int getSize(GraphRef G); int getParent(GraphRef G, int u); /* Pre: 1<=u<=n=getOrder(G) / int getDiscover(GraphRef G, int u); / Pre: 1<=u<=n=getOrder(G) / int getFinish(GraphRef G, int u); / Pre: 1<=u<=n=getOrder(G) / / Manipulation procedures / void addArc(GraphRef G, int u, int v); / Pre: 1<=u<=n, 1<=v<=n / void DFS(GraphRef G, ListRef S); / Pre: getLength(S)==getOrder(G) / / Other Functions / GraphRef transpose(GraphRef G); GraphRef copyGraph(GraphRef G); void printGraph(FILE out , GraphRef G); 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 GraphRef 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. You must #define constant macros for NIL and UNDEF which represent those values, and place the definitions in the file Graph.h. The function addArc() 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 function DFS(). 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: (i) getLength( S ) = n , and (ii) S contains some permutation of the integers {1, 2, ..., n } where n = getOrder(G). You are required to check the first precondition but not the second. Recall that DFS() calls the recursive algorithm Visit() (DFS-Visit in the text), and uses a variable called time which is static over all recursive calls to Visit. There are at least three possible approaches to implementing Visit(). You can define Visit() as a top level function in your graph implementation file (which is private and therefore not exported), and let time be a static variable whose scope is the entire file. This option has the drawback that other functions in the same file would have access to the global time variable. The second approach is to let time be a local variable in DFS(), then pass the address of time to Visit(), making it an input-output variable to Visit(). This is perhaps the simplest option, and is recommended. The third approach is to again let time be a local variable in DFS(), then nest 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 therefore 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. If you pick this option, first experiment with a few simple examples to make sure you know how it works. 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 (even with the –ansi flag for some reason). If you plan to develop your project on another platform, this approach may not be possible. 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

program for this project. To get full credit, your project must implement all required files and functions, compile without errors or warnings, produce correct output on our test files, and produce no memory leaks under bcheck. 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 ListRef argument (namely DFS). Therefore any file which #includes Graph.h (namely Graph.c, GraphTest.c, and FindComponents.c) must have the preprocessor directive #include for List.h appear before that for Graph.h, since the compiler must see the typedef which defines ListRef before it sees the prototype for function DFS(). Failure to do this will cause a syntax error. Do not resolve this syntax error by putting #include List.h inside Graph.h. Below is a Makefile which you may alter as you see fit:

Makefile for Graph ADT and related modules.

make makes FindComponents

make GraphTest makes GraphTest

make ListTest makes ListTest

make clean removes all object and executable files

FindComponents : FindComponents.o Graph.o List.o gcc -o FindComponents FindComponents.o Graph.o List.o GraphTest : GraphTest.o Graph.o List.o gcc -o GraphTest GraphTest.o Graph.o List.o ListTest : ListTest.o List.o gcc -o ListTest ListTest.o List.o FindComponents.o : FindComponents.c Graph.h List.h gcc -c -ansi -Wall FindComponents.c GraphTest.o : GraphTest.c Graph.h List.h gcc -c -ansi -Wall GraphTest.c ListTest.o : ListTest.c List.h gcc -c -ansi -Wall ListTest.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 -f FindComponents GraphTest ListTest FindComponents.o GraphTest.o ListTest.o Graph.o List.o Start Early and ask questions if anything is not completely clear.