Solutions of Tutorial Session 5 Graphs: A B C D
Solutions of Tutorial Session 5 Graphs: A B C D
GRAPHS
Question 1. A
A B C D E F G H
A 0 1 1 0 0 0 1 0
B 0 0 0 0 1 0 0 0
C 0 0 0 1 0 1 0 0
D 0 0 0 0 0 0 0 1
E 0 0 0 0 0 0 1 0
F 0 0 1 0 0 0 0 1
G 1 0 0 0 0 1 0 0
H 0 0 0 1 0 0 0 0
Question 2.
A B C D E F G H
A 0 1 1 0 0 0 1 0
B 1 0 0 0 1 0 0 0
C 1 0 0 1 0 1 0 0
D 0 0 1 0 0 0 0 1
E 0 1 0 0 0 0 1 0
F 0 0 1 0 0 0 1 1
G 1 0 0 0 1 1 0 0
H 0 0 0 1 0 1 0 0
a. Figure 4
Question 5.
a. algorithm DepthFirstSearch (val G <Digraph>, val source <Vertex>, val dest <Vertex>)
search for a path from source to dest using depth-first traversal and then print out the solution.
Pre
Post
1 loop (more vertex v in G)
1 predecessor (v) = null
2 unmark(v)
2 StackObj<Stack>
3 StackObj.Push(source)
4 loop (NOT StackObj.isEmpty())
1 w = StackObj.Pop() // include Top and Pop
2 if (w is dest)
1 StackResult<Stack> // temporary stack to print the solution
2 loop (NOT predecessor(w) is null)
1 StackResult.Push (w)
2 w = predecessor(w)
3 Print source
4 loop (NOT StackResult.isEmpty())
1 temp = StackResult.Pop()
2 Print temp
5 return
3 else if (vertex w is unmarked)
1 mark(w)
2 loop (more vertex x adjacent to w)
1 StackObj.Push(x)
2 Predecessor(x) = w
end DepthFirstSearch
b. algorithm BreadthFirstSearch (val G <Digraph>, val source <Vertex>, val dest <Vertex>)
search for a path from source to dest using breadth-first traversal and then print out the solution.
Pre
Post
1 loop (more vertex v in G)
1 predecessor (v) = null
2 unmark(v)
3 QueueObj<Queue>
3 QueueObj.EnQueue(source)
4 loop (NOT QueueObj.isEmpty())
1 w = QueueObj.DeQueue() // include QueueFront and DeQueue
2 if (w is dest)
1 StackResult<Stack> // temporary stack to print the solution
2 loop (NOT predecessor(w) is null)
1 StackResult.Push (w)
2 w = predecessor(w)
3 Print source
4 loop (NOT StackResult.isEmpty())
1 temp = StackResult.Pop()
2 Print temp
5 return
3 else if (vertex w is unmarked)
1 mark(w)
2 loop (more vertex x adjacent to w)
1 QueueObj.EnQueue(x)
2 Predecessor(x) = w
end BreadthFirstSearch
c. DepthFirst Search:
A-> B -> E -> G -> F -> H -> J -> L -> O
O Top of Stack
N
M
L J
K K
J H H
I I I
H F F F
G G G G
F E E E E
E E E E E
G C C C C C
F F F F F F
E B B B B B B
C C C C C C C
A B A A A A A A A
Figure 5
BreadthFirst Search:
A -> B -> E -> F -> H -> J -> L-> O
Rear
H
F G
G E E
F C C F O
G B G C E L N
E D G F G C J K M L L
C B D B F C I H H J J O L
A B A E B G B G F J H L J N O Front
d. algorithm simulate (val G <Digraph>, val source <Vertex>, val dest <Vertex>)
search for a path from source to dest using depth-first traversal and then print out the solution.
Pre
Post
1 loop (more vertex v in G)
3 predecessor (v) = null
4 unmark(v)
5 StackObj<Stack>
3 StackObj.Push(source)
4 loop (NOT StackObj.isEmpty())
3 w = StackObj.Pop() // include Top and Pop
4 if (w is dest)
1 StackResult<Stack> // temporary stack to print the solution
2 loop (NOT predecessor(w) is null)
1 StackResult.Push (w)
2 w = predecessor(w)
3 Print source
4 loop (NOT StackResult.isEmpty())
1 temp = StackResult.Pop()
2 Print temp
5 return
3 else if (vertex w is unmarked)
1 mark(w)
2 print w // print the current move
3 loop (more vertex x adjacent to w
AND x is unmarked)
1 StackObj.Push(w)
2 StackObj.Push(x)
3 Predecessor(x) = w
4 else
1 print “back to” + w
end simulate
Question 6.
Figure 6
b. Represent the map in an adjacency list
A -> (B,12) -> (E,18)
B -> (A,12) -> (C,3) -> (D,3)
C-> (B,3) -> (D,4) -> (J,19)
D -> (B,3) -> (C,4) -> (E,7) -> (I,21)
E -> (A,18) -> (D,7) -> (F,18) -> (G,31)
F -> (E,18) -> (G,12) -> (H,3)
G -> (E,31) -> (F,12) -> (Q,35)
H -> (F,3) -> (I,13) -> (M,9)
I -> (D,21) -> (H,13) -> (J,4) -> (L,6)
J -> (C,19) -> (I,4) -> (K,7)
K -> (J,7) -> (L,5) -> (O,14)
L -> (I,6) -> (K,5) -> (N,6)
M -> (H,9) -> (N,9) -> (Q,24)
N -> (M,9) -> (P,6)->(L,6)
O -> (K,14) -> (P,6) -> (Q,7)
P -> (N,6) -> (O,6) -> (Q,8)
Q -> (G,35) -> (M,24) -> (O,7) -> (P,8)
A B C D E F G H I J K L M N O P Q
A 0 1 1 c. Represent the
2 8 map in an
B 1 0 3 3 adjacency
2 matrix
C 3 0 4 1
9
D 3 4 0 7 2
1
E 1 7 0 1 3
8 8 1
F 1 0 1 3
8 2
G 3 1 0 35
1 2
H 3 0 1 9
3
I 2 1 0 4 6
1 3
J 1 4 0 7
9
K 7 0 5 1
4
L 6 5 0 6
M 9 0 9 24
N 6 9 0 6
O 1 0 6 7
4
P 6 6 0 8
Q 3 2 7 8 0
5 4
d. algorithm ShortestPath(val source <Vertex>, val dest <Vertex>, val G <Digraph>)
1 listOfShortestPath.clear()
2 Add source to set S
3 loop (more vertex v in digraph) // Initiate all distances from source to v
1 distanceNode.destination= v
2 distanceNode.distance= weight of edge(source, v) // =∞ if source is not connected to v
3 listOfShortestPath.Insert(distanceNode)
4 predecessor(v) = null
4 loop(more vertex not in S) // Add one vertex v to S on each step.
1 minWeight= infinity // Choose vertex v with smallest distance.
2 loop (more vertex w not in S)
1 Find the distance x from source to w in listOfShortestPath
2 if(x < minWeight)
1 v=w
2 minWeight= x
3 Add v to S.
4 if (v is dest)
5 StackResult<Stack> // temporary stack to print the solution
1 loop (NOT predecessor(v) is null)
1 StackResult.Push (v)
2 v = predecessor(v)
3 Print source
4 loop (NOT StackResult.isEmpty())
1 temp = StackResult.Pop()
2 Print temp
5 return
5 loop (more vertex w not in S) // Update distance sof all w not in S
1 Find the distance x from source to w in listOfShortestPath
2 alt = minWeight+ weight of edge from v to w
3 if(x > alt)
1 Update distance from source to w in listOfShortestPath to alt
2 predecessor(w) = v
end ShortestPath
Appendix A – An implementation of topological sorting
Given a directed graph G whose vertex set V = {v1,v2,…vn}. The topological order list L on V can be
found applying the following steps:
1
0 2
3 4
Figure 7
Similarly, in the next step we choose i = 2 and put i into L. L becomes (4,2) and deg = {0,2,0,1,0}.
Next, we choose i = 0. L becomes (4,2,0) and deg = {0,1,0,0,0}.
Next, we choose i = 3. L becomes (4,2,0,3) and deg = {0,1,0,0,0}.
The last one to be put into L is 1, and the final topological list L to be found is (4,2,0,3,1)