Question
Help with Q3 please!
3 (9 pts) For the graph G (VE) in question 2 (above), construct the adjacency lists for G (using alphabetical ordering) and t
2 (9 pts) Perform a dfs on the following graph G starting at the vertex S; whenever there is a choice of vertices, pick the o
0 0
Add a comment Improve this question Transcribed image text
Answer #1

As I can see the graph for Question 3 , the first thing that comes to my mind is to make a adjacency Matrix which will simplify the graph the graph and can see each vertex clearly , this will also help us to reverse the graph.

So the adjacency Matrix for the graph is

| 이 (0) A (0) | 0 | B(1) | 1 C(2) | 0 D(3) | 0 G(4) 10 H(5) | 0 | S (6) | 0 | (1) | (2) | (3) | (4) | (5) | (6) 0 | 1 | 0 0 1

in this , as you can see that (A,C)=1 that means Edge A points to C

and this is same for all edges where 1 is filled.

so now as we have this Matrix we can easily fill Adjacency List for G graph

E{A}={C}

E{B}={A,D}

E{C}={B}

E{D}={C,G}

E{G}={C,H}

E{H}={S}

E{S}={B}

So till here we have filled the adjacency table for G graph now we to make a function that will take Adjacency matrix and give us Gr(G reversed graph).

Pseudo Code:-

1) Function will be taking the 2 dimentional Matrix which is our Adjacency matrix as a parameter with a return type of 2D matrix so that it can return the reversed graph adjacency matrix .

Adjacency Matrix with Index

| 이 (0) A (0) | 0 | B(1) | 1 C(2) | 0 D(3) | 0 G(4) 10 H(5) | 0 | S (6) | 0 | (1) | (2) | (3) | (4) | (5) | (6) 0 | 1 | 0 0 1

2)Initialise the 2D matrix for reversed graph adjacency matrix of exactly same size as original matrix.

3)Now we have to traverse the whole Matrix using 2 'for' Nested Loop or any method of your choice.

4)In the Inner loop we will be checking for the value 1 and as we get the value 1 then we just have to exchange the x and y coordinates with each other and store 1 on that coordinates in Matrix that we have made for Reverse graph. Like this we will get the adjacency matrix for reversed graph.

G-Original Graph Matrix

RG-Reversed Graph Matrix - Initially RG will have all 0 values

suppose in this example we will get 1 at G[1][0] So we have to store 1 at RG[0][1].

G[0][2] is 1 therefore we to store 1 at GR[2][0]

5)After the nested are done traversing each matrix address, we will have our adjacency matrix for reversed graph done.

6)Now we just have to return this reversed Grap Matrix.

A (0) A(O)O B(1) 0 C(2) 1 D(3) 0 G(4) 0 H(5) O (6) 0 B C D G H (1) (2) (3) (4) (5) 1 OOO 0 1 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 1

7) End of function

So Now we got our adjacency Matrix for reversed Graph so now we can easily fill the table for Reversed graph

E{A}={B}

E{B}={C,S}

E{C}={A,D,G}

E{D}={B}

E{G}={D}

E{H}={G}

E{S}={H}

Here is the Reversed Graph:-

1585781770418_image.png

I have also added Programming in JAVA it will help you to understand it better.

CODE:-

public class ReverseGraph { public static int[][] reverse(int[][] G) int[][] RG=new int[G.length][G.length]; for(int i=0;i<G.

OUTPUT:- Adjacency Matrix for reversed Graph

<terminated> ReverseGraph [Java Appl 100 000 0 0 1 0 0 0 1 1 0 0 1 1 0 0 01 00000 0001000 0000100 OOO0010

Add a comment
Know the answer?
Add Answer to:
Help with Q3 please! 3 (9 pts) For the graph G (VE) in question 2 (above),...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT