Docsity

Programming Assignment 4: Finding Strong Components in a Directed Graph, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
Programming Assignment 4:
Strong Components
Computer Science 245
Spring 2005
1 The Assignment
For programming assignment 4 you should write a program that identifies the strong com-
ponents in a directed graph. The input to the program will be the adjacency matrix of a
directed graph whose vertices are the integers 0,1,...,n−1.The program’s output should
be the strong components of the input graph.
The file containing the adjacency matrix will contain the number of vertices non a line
by itself, and for each vertex, a line with a list of n0’s and 1’s. The ith value is 1 if there
is an edge from the vertex to vertex i, and the value is 0 otherwise. For example, suppose
we wanted to find the strong components for the graph show in figure 1. Then the input file
should contain
4
0 1 0 0
0 0 1 0
1 0 0 0
1 0 1 0
0 1
23
Figure 1: G
1
pf3
pf4
pf5

Partial preview of the text

Download Programming Assignment 4: Finding Strong Components in a Directed Graph and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Programming Assignment 4:

Strong Components

Computer Science 245

Spring 2005

1 The Assignment

For programming assignment 4 you should write a program that identifies the strong com- ponents in a directed graph. The input to the program will be the adjacency matrix of a directed graph whose vertices are the integers 0, 1 ,... , n − 1. The program’s output should be the strong components of the input graph. The file containing the adjacency matrix will contain the number of vertices n on a line by itself, and for each vertex, a line with a list of n 0’s and 1’s. The ith value is 1 if there is an edge from the vertex to vertex i, and the value is 0 otherwise. For example, suppose we wanted to find the strong components for the graph show in figure 1. Then the input file should contain

4 0 1 0 0 0 0 1 0 1 0 0 0 1 0 1 0

0 1

3 2

Figure 1: G

0 1

3 2

Figure 2: Gr

The 4 is the number of vertices. The line “0 1 0 0” shows the edges from vertex 0. The line “0 0 1 0” shows the edges from vertex 1, etc. The format of the output can vary. One approach is to just list the vertices belonging to each strong component on a separate line. For the example above, the output might be something like

The strong components are 0 1 2 3

The order of the components and the order of the vertices within a component are unimpor- tant. So equivalent, correct output might be

The strong components are 3 2 1 0

2 Algorithm

The following algorithm can be used to find the strong components of a directed graph.

  1. Perform a depth-first search of the graph G. During the search keep a counter that is initially set to zero. After completing the loop that visits a particular vertex’s neighbors, assign the current value in the counter to the vertex, and increment the counter.
  2. Form the graph Gr obtained from G by reversing the orientations on the edges. For example, for the graph shown G, shown in figure 1, the graph Gr is shown in figure 2.
  3. Perform a depth-first search of Gr. The outer, driver loop should go through the vertices in decreasing order according to the numbering generated in step 1.
  4. The strong components of G correspond to the trees in the depth-first spanning forest of Gr.

0, 1, 2 3

Figure 3: Reduced Graph

6 Extra Credit

In order to receive full credit your program only needs to print the vertex set for each strong component in the input graph. You can receive 10 points extra credit if your program also prints the reduced graph. Recollect that the vertices of the reduced graph are the strong components of the graph G, and there’s an edge joining the component W to the component U if there’s a cross-component edge from at least one vertex of W to a vertex of U. For example, the reduced graph of the graph shown in figure 1 is shown in figure 2. Your output of the reduced graph can either be an adjacency list or an adjacency matrix. However, it must be clear from your output what the vertices of the reduced graph are, i.e., which strong component of G corresponds to which vertex of the reduced graph.

7 Submitting your program

In order to submit your program you should copy all of your source files into a program subdirectory of your submit directory.

8 Due Date

In order to receive full credit, your program must be submitted electronically by 3:00 pm on Wednesday, May 4th, and you must turn in printouts of your source files by 6 pm on the 4th. You can receive half credit if you submit your program and turn in a print out after 3: pm Wednesday but before 3:00 pm Thursday. Programs submitted and printouts turned in later than 3:00 pm Thursday will receive no credit.

9 Grading

Correctness will be 70% of your grade. Does your program correctly implement the strong components algorithm using adjacency lists for internal storage? If your program uses adja- cency matrices instead of adjacency lists, your maximum possible score on correctness will be 55%. The following static features will be graded.

  1. Documentation will be 10% of your grade. Does your header documentation include the author’s name, the purpose of the program, and a description of how to use the program? Are the identifiers meaningful? Are any obscure constructs clearly ex- plained? Does the function header documentation explain the purpose of the function, its arguments, and its return value?
  1. Source format will be 10% of your grade. Is the indentation and naming of identifiers consistent? Have blank lines been used so that the program is easy to read? Are there any lines of source that are longer than 80 characters?
  2. Quality of solution will be 10% of your grade. Did you make a good choice of classes? Are any of your methods more than 40 lines? Are there long or multipurpose methods? Is your solution too clever — i.e., has the solution been condensed to the point where it’s incomprehensible? Is your implementation efficient?

10 Collaboration

It is OK for you to discuss solutions to this program with your classmates. However, no collaboration should ever involve looking at one of your classmate’s source programs! It is usually extremely easy to determine that someone has copied a program, even when the individual doing the copying has changed identifiers and comments. If we discover that someone has copied a program, the authors of both programs will receive an F on the assignment. Repeat offenders will receive an F in the course and be referred to the dean for additional disciplinary action.