DSA Assignment 9
DSA Assignment 9
Section: E-1
A B 5 C 8 D 4
B A 5 H 10
C A 8 E 1 G 9
8
D A 4 F 8 H 5
E C 1 J 10
1
F D 8 J 4 I 4
G C 9 J 6
H B 10 I 4 D 5 K 3
I F 3 H 4
J E 10 G 6 F 4
K H 3
3. What are the advantages and disadvantages of Adjacency matrix?
Advantages:
An adjacency matrix has fast lookups to check for presence or absence of a specific edge.
It have quick insertions and deletions of edges.
The main advantage of an adjacency matrix representation is that finding out whether or
not an edge exists, is easy.
Disadvantages:
The Iteration over all edges is slow.
Adjacency matrices needs more time to check for the edges.
It also uses excessive space, especially for graphs with many vertices
4. What are the advantages and disadvantages of Adjacency list?
Advantages:
Adjacency lists use memory in proportion to the number edges, which might save a lot of
memory.
It is fast to iterate over all edges.
Adjacency lists are a compact way of representing only existing edges.
Disadvantages:
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.
The main disadvantage of this representation is that finding the presence or absence of
specific edge is slightly slower than with the matrix.
8. What are the key ideas of Depth first search (DFS) and Breadth first
search (BFS). Give precise statements.
Pick a starting node and push all its adjacent nodes into a stack.
Pop a node from stack to select the next node to reach and push all its adjacent nodes into
a stack.
Repeat this process until the stack is empty. However, ensure that the nodes that are
visited are marked. This will prevent you from visiting the same node more than once.
Breadth-first search (BFS) is a traversing algorithm where we start traversing from a selected
node and traverse the graph layer wise by checking the neighbor nodes and then move
towards the next-level neighbor nodes.
First move horizontally and visit all the nodes of the current layer
Move to the next layer
11. Give the dry run of Kruskle algorithm to find MST for graph in figure
(1). Source vertex is A?
Step-1: A-B
B
Step-2: A-B-H
B
H
Step-3: A-B-H-K
K
B
H
Step-4: A-D
K
B
H
D
A
Step-5: A-D-F
K
B
H
D
A
F
Step-6: A-D-I
B
H
D I
A
F
Step-7: A-C
K
B
H
D I
A
F
K
Step-8: A-C-E
B
H
D I
A
F
E
Step-9: A-C-E-J
K
B
H
D I
A
F
J
E
Step-10: C-G K
B
H
D I
A
F
J
E
G
C
Ans: A path in a graph is a finite or infinite sequence of edges which connect a sequence
of vertices which are all distinct from one another. A path is a trail in which all vertices are
distinct. A trail is a walk in which all edges are distinct. In a directed graph, a directed path is
again a sequence of edges which connect a sequence of vertices.