0% found this document useful (0 votes)
2 views3 pages

Graph HW5 Full Solution

The document provides solutions to a graph homework assignment, covering various topics including adjacency lists and matrices for a binary tree, BFS and DFS algorithms for both directed and undirected graphs, and edge classification. It includes pseudocode for DFS, discovery and finish times for DFS traversal, and discusses maze solving techniques using DFS. The document emphasizes time complexity considerations and methods for efficiently exploring graph structures.

Uploaded by

diaayusuf7
Copyright
© © All Rights Reserved
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)
2 views3 pages

Graph HW5 Full Solution

The document provides solutions to a graph homework assignment, covering various topics including adjacency lists and matrices for a binary tree, BFS and DFS algorithms for both directed and undirected graphs, and edge classification. It includes pseudocode for DFS, discovery and finish times for DFS traversal, and discusses maze solving techniques using DFS. The document emphasizes time complexity considerations and methods for efficiently exploring graph structures.

Uploaded by

diaayusuf7
Copyright
© © All Rights Reserved
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/ 3

Graph HW#5 - Solutions

Q1: Adjacency list and matrix for a complete binary tree with 7 vertices (1 to 7):

Binary tree:

/\

2 3

/\/\

4 56 7

Adjacency List:

1: 2, 3

2: 4, 5

3: 6, 7

4:

5:

6:

7:

Adjacency Matrix:

1234567

1[0110000]

2[0001100]

3[0000011]

4[0000000]

5[0000000]

6[0000000]

7[0000000]

Q2: BFS from vertex 3 (Directed Graph):

Graph edges: 3->4, 3->5, 4->1, 5->2, 6->2


d (distance):

3: 0, 4: 1, 5: 1, 1: 2, 2: 2, 6: INF

pi (predecessor):

3: NIL, 4: 3, 5: 3, 1: 4, 2: 5, 6: NIL

Q3: BFS from vertex u (Undirected Graph):

Vertices: u, v, w, x, y, z, r, s, t

d:

u: 0, v: 1, w: 1, x: 2, y: 2, z: 2, r: 3, s: 3, t: 3

pi:

u: NIL, v: u, w: u, x: v, y: v, z: w, r: x, s: y, t: z

Q4: BFS on undirected graph (S is source):

Order of discovery: S, D, A, E, B, F, G, C

Q5: DFS using stack (non-recursive):

Pseudocode:

DFS(G):

for each vertex u in G.V:

u.color = WHITE, u.pi = NIL

time = 0

for each vertex u in G.V:

if u.color == WHITE:

STACK-DFS(u)

STACK-DFS(u):

create stack S

push u

while S not empty:

v = S.pop()
if v.color == WHITE:

v.color = GRAY

for each neighbor w of v:

if w.color == WHITE:

w.pi = v

push w

v.color = BLACK

Q6: DFS edge classification (print edges and type):

Modify DFS-VISIT to print edge (u,v) and type:

- Tree Edge: v is WHITE when discovered

- Back Edge: v is GRAY

- Forward/Cross Edge: v is BLACK

If G is undirected:

Only tree and back edges appear (since every edge is bidirectional)

Q7: DFS result on given directed graph:

Label discovery and finish times during DFS traversal.

E.g., d[u] = time of first visit, f[u] = time of finishing

Q8: DFS on alphabetically ordered graph:

Visit order: q, r, s, t, u, v, w, x, y, z

Label discovery and finish times; classify edges as Tree, Back, Forward, Cross

Q9: BFS using adjacency matrix:

Time Complexity: O(V^2)

To handle this, loop through row i of matrix to find adjacent vertices in O(V)

Q10: Maze solving with O(V+E) DFS:

Traverse each edge exactly once in each direction (like undirected DFS)

Maintain visited set, backtrack when no unvisited neighbor

This ensures full exploration and path out of maze

You might also like