Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
17 views6 pages

Graphs

The document contains a series of questions and answers related to data structures and algorithms, specifically focusing on graph representations such as adjacency matrices and lists. It discusses the advantages and disadvantages of these representations, the concepts of maximum edges in directed and undirected graphs, and the definitions of vertex degree, depth-first search (DFS), breadth-first search (BFS), minimum spanning trees (MST), and paths in graphs. Additionally, it includes a dry run of Kruskal's algorithm for finding the MST.

Uploaded by

Hasnain Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Graphs

The document contains a series of questions and answers related to data structures and algorithms, specifically focusing on graph representations such as adjacency matrices and lists. It discusses the advantages and disadvantages of these representations, the concepts of maximum edges in directed and undirected graphs, and the definitions of vertex degree, depth-first search (DFS), breadth-first search (BFS), minimum spanning trees (MST), and paths in graphs. Additionally, it includes a dry run of Kruskal's algorithm for finding the MST.

Uploaded by

Hasnain Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Roll No: 267-BSCS-2015

Section: E1 (Semester#4)
“Data Structure &Algorithm”
Submitted To: Sir Tauseef Iftikhar
Submitted By: Usama Shahid

GC
University Lahore.
Question 1:
Represent the given weighted graph in adjacency matrix.

Ans:

 Given graph in adjacency matrix:


A B C D E F G H I J K
A 0 5 8 4 0 0 0 0 0 0 0
B 5 0 0 0 0 0 0 10 0 0 0
C 8 0 0 0 1 0 9 0 0 0 0
D 4 0 0 0 0 8 0 5 0 0 0
E 0 0 1 0 0 0 0 0 0 10 0
F 0 0 0 8 0 0 0 0 3 4 0
G 0 0 9 0 0 0 0 0 0 6 0
H 0 10 0 5 0 0 0 0 4 0 3
I 0 0 0 0 0 3 0 4 0 0 0
J 0 0 0 0 10 4 6 0 0 0 0
K 0 0 0 0 0 0 0 3 0 0 0

Question 2:
Represent the given weighted graph in adjacency list.

Ans:

 Given graph in adjacency list:


A B 5 C 8 D 4
B A 5 H 10
C A 8 E 1
G 9
D A 4 F 8
H 5
E C 1 J 10
F D 8 I 3
J 4
G C 9 J 6
H B 10 D 5
I 4 K 3
I F 3 H 4
J E 10 F 4
G 6
K H 3
Question 3:
What are the advantages and disadvantages of Adjacency matrix.

Ans:
Advantages:
 Adjacency matrix is very convenient to work with.
 Add or remove an edge can be done in quick time.
 The same time is required to check, if there is an edge between two vertices.

Disadvantages:
 Adjacency matrix consumes huge amount of memory for storing big graphs.
 Adjacency matrix requires huge efforts for adding or removing a vertex.
 In many algorithms you need to know the edges, adjacent to the current vertex.

Question 4:
What are the advantages and disadvantages of Adjacency list.

Ans:
Advantages:
 Adjacent list allows us to store graph in more compact form, than adjacency matrix.
 It is frequent to iterate over all edges of a graph.
 Adjacent list allows to get the list of adjacent vertices in quick time, which is a big
advantage for some algorithms.

Disadvantages:
 Adding or removing an edge to or from adjacent list, is not so easy as for adjacency
matrix.
 It is harder to check whether a given edge is in a graph, because you have to search
through the appropriate list to find the edge.

Question 5:

There are how many edges in an undirected graph at most.

Ans:

If you have N nodes, there are (N – 1)/2 directed edges than can lead from it. Therefore,
the maximum number of edges is N*(N – 1)/2.

Question 6:
There are how many edges in a directed graph at most.

Ans:

If you have N nodes, there are (N – 1) directed edges than can lead from it. Therefore,
the maximum number of edges is N*(N – 1).

Question 7:

What is degree of vertex in a directed graph and in an undirected graph.

Ans:

Degree of vertex in directed graph has two types:

 Indegree: No of edges with their head towards the vertex.


 Outdegree: No of edges with their tail towards the vertex.

Degree of vertex in undirected graph is the number of edges incident with its,

 Loops (which counts twice)


 Degree of vertex v is denoted deg(v)

Question 8:

What are the key ideas of Depth first search (DFS) and Breadth first search (BFS). Give
precise statements.

Ans:

Depth First Search:

A depth first search (DFS) explores a path all the way to a leaf before backtracking and
exploring another path. Backtrack means that when you are moving forward and there are no
more nodes along the current path, you move backwards on the same path to find nodes to
traverse.

Breadth First Search:

A breadth first search (BFS) explore nodes nearest the root before exploring nodes
further away.

Question 9:
Write the sequence of vertices traversed using DFS and BFS for graph in figure (1).
Source vertex is A

Ans:

 By DFS:
Nodes are explored in the sequence of:
A, C, G, J, E, D, F, I, B, H, K
 By BFS:
Nodes are explored in the sequence of:
A, C, B, E, D, G, F, H, J, I, K

Question 10:

Define minimum spanning tree (MST).

Ans:

The cost of the spanning tree is the sum of the weights of all the edges in the tree. There
can be many spanning trees. Minimum spanning tree is the spanning tree where the cost is
minimum among all the spanning trees. There also can be many minimum spanning trees.

Question 11:

Give the dry run of Kruskle algorithm to find MST for graph in figure (1). Source vertex
is A.

Ans:

B
H

D I
A
F

J
E

G
C

Procedure:
1. A-B
2. A-C
3. A-D
4. B-H
5. C-E
6. C-G
7. D-H: (Tree is closing by moving from D to H, In MST, closed trees are not allowed.)
8. D-F
9. E-J
10. F-I
11. F-J: (Tree is closing by moving from D to H, In MST, closed trees are not allowed.)
12. G-J: (Tree is closing by moving from D to H, In MST, closed trees are not allowed.)
13. H-K
14. H-I: (Tree is closing by moving from D to H, In MST, closed trees are not allowed.)

Question 12:

Define path for a vertex in a graph.

Ans:

 A path in a graph is a finite or infinite sequence of edges which connect a sequence


of vertices which are all different from one another.
 A path is a trail in which all vertices are different. A trail is a walk in which all edges
are different. In a directed graph, a directed path is again a sequence of edges which
connects a sequence of vertices.

You might also like