Professional Documents
Culture Documents
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 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.