This preview shows page 1. Sign up to view the full content.
Unformatted text preview: ns
in line 3 are executed once. Thus line 3 is executed n times (since graph is connected, and there
are n vertices), and hence at most C ′ n opertations, for some constant C ′ , are incurred by line 3.
The for loop in line 4 is executed
deg(x) = 2m
x ∈V times, and each time that the for loop is iterated, a constant number of operations are performed.
Thus, the number of steps incurred by the for loop in line 4 is at most C ′′ m for some constant C ′′ .
This shows that the total number of operations required by BFS is at most C (n + m) for some
constant C . In timecomplexity, we use O(m + n), denoting that the timecomplexity of BFS is
1 linear in m and n (as opposed to square, cubic, exp, or log). (For example, a lineartime algorithm
is obviously better than an exponentialtime algorithm, and the diﬀerence is huge when the problem
size is big.)
Question 2. (2 points) (a) Exercise 2.3.3. Use Kruskal’s algorithm to ﬁnd a minimum weight
spanning tree for a graph G with 5 vertices whose edge weights are given by the matrix below: 0 3 5 11 9
3 0 3 9 8 5 3 0 ∞ 10 11 9 ∞ 0 7 9 8 10 7 0
Note: For i = j , the ij th entry represents the weight of the edge joining vertices vi and vj . (Since
loops are irrelevant in spanning trees, we use 0 for the diagonal entries.)
(b) Exercise 2.3.5. (altered) The following matrix gives the edge weights a ﬁvevertex digraph: 0 10 20 ∞ 17 7 0 5 22 33 14 13 0 15 27 30 ∞ 17 0 10
∞ 15 12 8 0
For i = j , the ij th entry represents the weight of the edge (vi , vj ). Use Dijkstra’s algorithm to
ﬁnd a shortest paths spanning tree, showing shortest paths from v1 to all other vertices. Find also
d(v1 , vi ), for i = 1, 2, . . . , 5.
Solution. Assume the vertices are labeled as a, b, c, d, e. The minimum weight spanning tree I found
is given below.
a
11
00
11
00
11
00 b
3,1 00
11
11
00
11
00 8,5 d
1
0
1
0
1
0
7,4 5,3 3,2 11
00
11
00
11
00 1
0
1
0
1
0 c e The dotted edge(s) are those examined but not added to the tree....
View
Full
Document
This note was uploaded on 01/13/2014 for the course MAD 5305 taught by Professor Suen during the Spring '12 term at University of South Florida  Tampa.
 Spring '12
 Suen

Click to edit the document details