0% found this document useful (0 votes)
72 views10 pages

Solutions of Tutorial Session 5 Graphs: A B C D

The document provides solutions to tutorial questions on graphs. It includes: 1. Listing non-cyclic paths between nodes in graphs and representing graphs using adjacency matrices and lists. 2. Calculating shortest paths between nodes in a graph represented using an adjacency matrix. 3. Explaining topological sorting of graphs using in-degrees of nodes and putting nodes in a list. 4. Comparing depth-first and breadth-first search algorithms on graphs and printing the paths found between source and destination nodes.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views10 pages

Solutions of Tutorial Session 5 Graphs: A B C D

The document provides solutions to tutorial questions on graphs. It includes: 1. Listing non-cyclic paths between nodes in graphs and representing graphs using adjacency matrices and lists. 2. Calculating shortest paths between nodes in a graph represented using an adjacency matrix. 3. Explaining topological sorting of graphs using in-degrees of nodes and putting nodes in a list. 4. Comparing depth-first and breadth-first search algorithms on graphs and printing the paths found between source and destination nodes.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

SOLUTIONS OF TUTORIAL SESSION 5

GRAPHS

Question 1. A

a1. All noncyclic paths from A to D B C D


A-> B -> E-> G -> F -> C -> D
A-> B -> E-> G -> F -> H -> D
E F H
A-> C -> D
A -> C -> F -> H -> D G
A -> G -> F -> C -> D Figure 1
A -> G -> F -> H -> D
a2. All noncyclic paths from B to H
B -> E -> G -> A -> C -> D -> H
B -> E -> G -> A -> C -> F -> H
B -> E -> G -> F -> C -> D -> H
B -> E -> G -> F -> H
a3. All noncyclic paths from E to C
E-> G -> A -> C
E -> G -> F ->C
b. Give the adjacency matrix representation of the graph.

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

c. Give the adjacency list representation of the graph.


A-> B -> C -> G
B -> E
C -> D -> F
D -> H
E -> G
F -> C -> H
G -> A -> F
H -> D
d. Give the depth-first traversal of the graph (supposed we start from A).
Using Alphabetic order: A -> B ->E -> G -> F -> C -> D -> H
e. Give the breadth-first traversal of the graph (supposed we start from A).
Using Alphabetic order: A -> B ->C -> G -> E -> D -> F -> H

Question 2.

a1. All noncyclic paths from A to D


A-> B -> E-> G -> F -> C -> D
A-> B -> E-> G -> F -> H -> D
A-> C -> D
A -> C -> F -> H -> D
A -> G -> F -> C -> D
A -> G -> F -> H -> D
a2. All noncyclic paths from B to H
B -> A -> C -> D -> H
B -> A -> C -> F -> H
B -> A -> G -> F -> C -> D -> H
B -> A -> G -> F -> H
B -> E -> G -> A -> C -> D -> H
B -> E -> G -> A -> C -> F -> H
B -> E -> G -> F -> C -> D -> H
B -> E -> G -> F -> H
a3. All noncyclic paths from E to C
E -> B -> A -> C
E -> B -> A -> G -> F -> C
E -> B -> A -> G -> F -> H -> D -> C
E-> G -> A -> C
E -> G -> F -> C
E -> G -> F -> H -> D -> C
b. Give the adjacency matrix representation of the graph.

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

c. Give the adjacency list representation of the graph.


A -> B -> C -> G
B -> A -> E
C -> A -> D -> F
D -> C -> H
E -> B -> G
F -> C -> G -> H
Figure 2
G -> A -> E -> F
H -> D -> F
d. Give the depth-first traversal of the graph (supposed we start from A).
Using Alphabetic order: A -> B -> E -> G -> F -> C -> D -> H
e. Give the breadth-first traversal of the graph (supposed we start from A).
Using Alphabetic order: A -> B -> C -> G -> E -> D -> F -> H
Question 3.

a. Give the adjacency matrix representation of the graph.


A B C D E F G H
A 0 4 3 0 0 0 1 0
B 4 0 0 0 3 0 0 0
C 3 0 0 8 0 5 0 0
Figure 3
D 0 0 8 0 0 0 0 5
E 0 3 0 0 0 0 6 0
F 0 0 5 0 0 0 2 7
G 1 0 0 0 6 2 0 0
H 0 0 0 5 0 7 0 0

b. Give the adjacency list representation of the graph.


A -> (B,4) -> (C,3) -> (G,1)
B -> (A,4) -> (E,3)
C -> (A,3) -> (D,8) -> (F,5)
D -> (C,8) -> (H,5)
E -> (B,3) -> (G,6)
F -> (C,5) -> (G,2) -> (H,7)
G -> (A,1) -> (E,6) -> (F,2)
H -> (D,5) -> (F,7)
c. Find the shortest path between node B and all other nodes in the above graph.
B -> A (length: 4)
B -> A -> C (length: 7) A
B -> A -> C -> D (length: 15)
B C D
B -> E (length: 3)
B -> A -> G -> F (length: 7)
B -> A -> G (length: 5) E F H
B -> A -> G -> F -> H (length: 14)
G
Question 4.

a. Figure 4

Step A B C D E F G H Put into L


1 0 1 1 1 1 2 3 2 A
2 0 0 0 1 1 2 2 2 C
3 0 0 0 0 1 1 2 2 D
4 0 0 0 0 1 1 1 1 B
5 0 0 0 0 0 1 1 1 E
6 0 0 0 0 0 1 0 1 G
7 0 0 0 0 0 0 0 1 F
8 0 0 0 0 0 0 0 0 H

A -> C -> D -> B -> E -> G -> F- > H

b. Write pseudocode for the algorithm given in Appendix A.


algorithm TopologicalSorting (val G <Digraph>)
Pre G is acyclic
Post
Return A list contains all vertices which are arranged by the topological order
1. L = new List
2. loop (more vertex vi in G)
1. d[i] = the indegree of vi
3. loop until all vertices of G are put into L
1. Find the largest i such that d[i] = 0 and vi has not been put into L
2. L.addlast(vi)
3. loop (more vertex vj adjacent to vi)
1. if (vj has not been put in L)
d[j] = d[j] - 1
4. return L
end TopologicalSorting

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:

Initially, make L empty


Construct a vertor deg = {d1,d2,…,dn} where di is the indegree of vi.
Repeat the following tasks until all of vertices in V are put into L:
- Find the largest i such that di = 0 and vi has not been put into L
- Put vi into L
- For each vj such that vj is adjacent to vi and vj has not been put in L, make dj = dj-1

1
0 2

3 4

Figure 7

Example 1: Consider the


graph in Figure 7, we have V
= {0,1,2,3,4}, the initial deg
= {0,4,1,1,0} and L = ().

At the beginning, we have d0


= 0 and d4 = 0, in the
meantime neither 0 nor 4 have been put in L. Thus we choose i = 4 (the largest) and put 4 into L.
Thus, L becomes (4). Since 1 and 2 are adjacent to 4, we decrease the value of d1 and d2
accordingly. Therefore, deg becomes {0,3,0,1,0}.

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)

You might also like