0% found this document useful (0 votes)
14 views8 pages

DSA Assignment 9

The document is an assignment submitted by Muhammad Hasnain Khan on data structures and algorithms, focusing on representing a weighted graph using adjacency matrices and lists. It discusses the advantages and disadvantages of both representations, the maximum number of edges in directed and undirected graphs, and key concepts of depth-first search (DFS) and breadth-first search (BFS). Additionally, it defines minimum spanning trees (MST) and provides a dry run of Kruskal's algorithm for finding an 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)
14 views8 pages

DSA Assignment 9

The document is an assignment submitted by Muhammad Hasnain Khan on data structures and algorithms, focusing on representing a weighted graph using adjacency matrices and lists. It discusses the advantages and disadvantages of both representations, the maximum number of edges in directed and undirected graphs, and key concepts of depth-first search (DFS) and breadth-first search (BFS). Additionally, it defines minimum spanning trees (MST) and provides a dry run of Kruskal's algorithm for finding an 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/ 8

Government College University

Department Of Computer Science


Data Structures & Algorithm
Assignment 8: Solution

Submitted By: Muhammad Hasnain Khan

Submitted To: Mr. Tauseef Iftikhar

Section: E-1

Roll No: 279-BSCS-2015


Dated: 31-5- 2017
1. Represent the given weighted graph in adjacency matrix?

DIAGONAL MATRIX FOR THE FIGURE 1


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

2. Represent the given weighted graph in adjacency list?

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.

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


Ans: If we 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.

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


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

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

 Degree of vertex in a directed graph is the (In-degree + Out-degree).

In-Degree: Number of edges going into a node.

Out-Degree: Number of edges coming out of a graph.

 Degree of a vertex in an undirected graph is the number of edges incident on it.

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

Depth first search:

 Depth-first search (DFS) is a recursive algorithm for traversing or


searching tree or graph data structures.

 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:

 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

10. Define minimum spanning tree (MST)?


Ans: A minimum spanning tree (MST) is a subset of the edges of a connected edge-
weighted undirected graph that connects all the vertices together, without any cycles and with
the minimum possible total edge weight. Simply, it is a spanning tree whose sum of edge
weights is as small as possible.

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

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 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy