Tut5 Sol

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Faculty of Computer Science and Engineering Department of Computer Science

DATA STRUCTURES & ALGORITHMS


Tutorial 5 Questions Graph
Required Questions Question 1. Given the graph in Figure 1, complete the following tasks: a. Find a1. All noncyclic paths from A to D a2. All noncyclic paths from B to H a3. All noncyclic paths from E to C b. Give the adjacency matrix representation of the graph. c. Give the adjacency list representation of the graph. d. Give the depth-first traversal of the graph (supposed we start from A). e. Give the breadth-first traversal of the graph (supposed we start from A). Answer: a1. All noncyclic paths from A to D: ABEGFCD ABEGFCHD ABEGFHD ABEGFHCD ACD ACFHD ACHD a2. All noncyclic paths from B to H: BEGFCDH
A B C D

E G

Figure 1

Faculty of Computer Science and Engineering Department of Computer Science BEGFCH BEGFH a3. All noncyclic paths from E to C: EGFC EGFHC b. Give the adjacency matrix representation of the graph. A B C D E F G A 0 1 1 0 0 0 0 B 0 0 0 0 1 0 0 C 0 0 0 1 0 1 0 D 0 0 0 0 0 0 0 E 0 0 0 0 0 0 1 F 0 0 1 0 0 0 0 G 0 0 0 0 0 1 0 H 0 0 1 1 0 0 0 c. Give the adjacency list representation of the graph. ABC BE C D F H D H E G F C H G F H C D d. Give the depth-first traversal of the graph (supposed we start from A). Using alphabetic order: A C H F D B E G e. Give the breadth-first traversal of the graph (supposed we start from A). Using alphabetic order: A B C E D F H G Question 2. Given the graph in Figure 2, complete the following tasks:

H 0 0 1 1 0 1 0 0

Figure 2

Faculty of Computer Science and Engineering Department of Computer Science

a. Give the adjacency matrix representation of the graph. b. Give the adjacency list representation of the graph. c. Find the shortest path between node E and all other nodes in the above graph. Answer: 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 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 E and all other nodes in the above graph.

Shortest Path from E to A: E B A or E G A (7)

Faculty of Computer Science and Engineering Department of Computer Science Shortest Path from E to B: E B (3) Shortest Path from E to C: E B A C or E G A C (10) Shortest Path from E to D: E B A C D or E G A C D (18) Shortest Path from E to F: E G F (8) Shortest Path from E to G: E G (6) Shortest Path from E to H: E G F H (15)
A B C D

Question 3. Given the graph in Figure 3, generate the corresponding topology order using depth-first search and breadth-first search.

E G

Answer:A:marked. Using depth-first search: (Slides 40-44) Step 1 2 3 4 5 6 7 8 Stack H F F F G G G G G E E E E E E B B B B B B B A A A A A A A A Add First List H F G Result List: A C D B E G F H 9 10 11

Figure 3

12

13

14

E B A E

B A B

C A

D C A D

C A C

A A

Using Breadth-first search: (Slides 45-47) Step A B C D E F G H Add Last List 0 0 1 1 1 1 2 3 4 1 0 0 0 1 1 2 2 2 A 2 0 0 0 1 0 2 2 2 B 3 0 0 0 0 0 1 2 2 C 4 0 0 0 0 0 1 1 2 E 5 0 0 0 0 0 1 0 1 D 6 0 0 0 0 0 0 0 1 G 7 0 0 0 0 0 0 0 0 F 8 0 0 0 0 0 0 0 0 H Result List: A B C E D G F H --End--

Queue A BC CE ED D G F H

You might also like