You are on page 1of 3

COMP4204: Advanced Data structure and Algorithms

Tutorial 6
(Graphs)
1. Prove by induction that a graph with n vertices has at most n(n-1)/2 edges.

2 B G
10 12
2
A 7
5 2 C B D 1
3 1
E 5
D A 2 F
5 1
7 6
9 2
8 H 3
C E
F 3 3
I
4
G 11 10

2. For the undirected weighted graphs shown above:


a. Represent the graphs using the adjacency matrix representation
b. Represent the graphs using the adjacency list representation.
c. If a pointer requires four bytes, a vertex label requires two bytes, and an edge weight requires
two bytes, which representation requires more space for each graph?
d. If a pointer requires four bytes, a vertex label requires one byte, and an edge weight requires two
bytes, which representation requires more space for each graph?
e. Show the DFS tree for each graph starting at Vertex A. Show the content of the Stack for each
iteration.
f. Show the BFS tree for each graph starting at Vertex A. Show the content of the Queue after each
iteration.
g. Show the shortest paths generated by running Dijkstra’s shortest-paths algorithm on the graphs
when starting at Vertex A. Show the D values as each vertex is processed, as in Figure 11.19 of
the textbook page 403.
h. List the order in which the edges of the graphs are visited when running Prim’s MST algorithm
starting at Vertex F. Show the final MST.
i. List the order in which the edges of the graphs are visited when running Kruskal’s MST algorithm.
Each time an edge is added to the MST, show the result on the equivalence array, (e.g., show the
array as in Figure 6.7 of the textbook page 211).
3. Write an algorithm to find the longest path in a DAG, where the length of the path is measured by
the number of edges that it contains. What is the asymptotic complexity of your algorithm?
4. Write an algorithm to find a maximum cost spanning tree, that is, the spanning tree with highest
possible cost.
5. List at least two topological orderings of the vertices on each of the following graphs.

2 B G
10 12
2
A 7
5 2 C B D
3 1 1
E 5
D 5 A F
1 2
7 6
8 9 2
H
3
C E
F 3 I 3
4
G 11 10
6. In a city there are N farms, each of which is in need of a water supply. It costs w[i] Rials to build a
well at farm i, and it costs c[i][j] to build a pipe in between farms i and j. A farm can receive water if
either there is a well built there or there is some path of pipes to a farm with a well. Design an
algorithm to find the minimum amount of money needed to supply every farm with water.
7. Consider a set of 5 towns. The cost of construction of a road between towns i and j is aij. Find the
minimum cost road network connecting the towns with each other if they have the following
construction costs.
0 3 5 11 9
3 0 3 9 8
5 3 0 ∞ 10
11 9 ∞ 0 7
9 8 10 7 0
8. USA Computing Olympiad problem: Simplifying the Farm
Farmer John has been taking an evening algorithms course at his local university, and he has
just learned about minimum spanning trees. However, Farmer John now realizes that the design
of his farm is not as efficient as it could be, and he wants to simplify the layout of his farm. The
farm is currently arranged like a graph, with vertices representing fields and edges representing
pathways between these fields, each having an associated length. Farmer John notes that for
each distinct length, at most three pathways on his farm share this length. FJ would like to
remove some of the pathways on his farm so that it becomes a tree -- that is, so that there is one
unique route between any pair of fields. Moreover, Farmer John would like this to be a minimum
spanning tree -- a tree having the smallest possible sum of edge lengths. Help Farmer John
compute not only the sum of edge lengths in a minimum spanning tree derived from his farm
graph, but also the number of different possible minimum spanning trees he can create.

INPUT FORMAT:
 Line 1: Two integers N and M (1 <= N <= 40,000; 1 <= M <= 100,000), representing the
number of vertices and edges in the farm graph, respectively. Vertices are numbered as 1 ...
N.
 Lines 2 ... M +1: Three integers ai, bi and ni (1 <= ai, bi <= N; 1 <= ni <= 1,000,000)
representing an edge from vertex ai to bi with length ni. No edge length ni will occur more
than three times.
SAMPLE INPUT (file simplify.in):
45
121
341
132
142
232

OUTPUT FORMAT:
 Line 1: Two integers representing the length of the minimal spanning tree and the number
of minimal spanning trees (mod 1,000,000,007).
SAMPLE OUTPUT (file simplify.out):
43
OUTPUT DETAILS:
Picking both edges with length 1 and any edge with length 2 yields a minimum spanning tree of
length 4.

You might also like