0% found this document useful (0 votes)
54 views24 pages

MFCS Unit 5

Uploaded by

Krishna
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)
54 views24 pages

MFCS Unit 5

Uploaded by

Krishna
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/ 24

Qn: What are the various representations for a graph?

Give their advantages &


Disadvantages.
Ans. A graph can be represented using 3 data structures- adjacency matrix, adjacency list
and adjacency set.

In case of a directed graph, if there is an edge from vertex i to vertex j then


we just assign adj[i][j]=1
The adjacency matrix for the above example graph is:

1. The major advantage of matrix representation is that the calculation of paths and
cycles can easily be performed using well known operations of matrices

2. Adjacency matrix representation can be used for manipulation of graphs easily in programs
Adjacency List:
An array of lists is used. The size of the array is equal to the number of
vertices. Let the array be an array[]. An entry array[i] represents the list of
vertices adjacent to the ith vertex. This representation can also be used to
represent a weighted graph. The weights of edges can be represented as
lists of pairs. Following is the adjacency list representation of the above
graph.

Advantages of Graph:
 By using graphs we can easily find the shortest path, neighbors of
the nodes, and many more.
 Graphs are used to implement algorithms like DFS and BFS.
 It is used to find minimum spanning tree which has many practical
applications.
 It helps in organizing data.
 Because of its non-linear structure, helps in understanding complex
problems and their visualization.
 Graphs can handle large amounts of data and can easily be
distributed across multiple machines.
 Graphs can be used to model many different types of real-world
relationships and connections.
 Graphs are well suited to handle sparse data.
 Graphs can support multiple types of relationships between entities,
such as one-to-one, one-to-many, and many-to-many.
 It makes data more easily understandable.
 It saves time.
 It makes the comparison of data more efficient.


Disadvantages of Graph:
 Graphs use lots of pointers which can be complex to handle.
 It can have large memory complexity.
 If the graph is represented with an adjacency matrix then it does not
allow parallel edges and multiplication of the graph is also difficult.
 Some graph algorithms have high time complexity, which can slow
down the performance of a system.
 Graphs can have cyclic relationships, which can make it difficult to
traverse or process the data.
 Graphs may not have built-in support for advanced analytics such
as machine learning or data mining.

Qn Differentiate BFS and DFS


Ans:

BFS DFS
BFS Stands for “Breadth First Search”. DFS stands for “Depth First Search”.
BFS starts traversal from the root node
DFS starts the traversal from the root node and
and then explore the search in the level
explore the search as far as possible from the root
by level manner i.e. as close as possible
node i.e. depth wise.
from the root node.
Breadth First Search can be done with
Depth First Search can be done with the help of
the help of queue i.e. FIFO
Stack i.e. LIFO implementations.
implementation.
This algorithm works in two stages – in the first
This algorithm works in single stage. The
stage the visited vertices are pushed onto the stack
visited vertices are removed from the
and later on when there is no vertex further to visit
queue and then displayed at once.
those are popped-off.
BFS is slower than DFS. DFS is more faster than BFS.
BFS requires more memory compare to
DFS require less memory compare to BFS.
DFS.
Applications of BFS
Applications of DFS
> To find Shortest path
> Useful in Cycle detection
> Single Source & All pairs shortest
> In Connectivity testing
paths
> Finding a path between V and W in the graph.
> In Spanning tree
> useful in finding spanning trees & forest.
> In Connectivity
DFS in not so useful in finding shortest path. It is
BFS is useful in finding shortest
used to perform a traversal of a general graph and
path.BFS can be used to find the shortest
the idea of DFS is to make a path as long as
distance between some starting node and
possible, and then go back (backtrack) to add
the remaining nodes of the graph.
branches also as long as possible.
Example :
Example :
Qn: Give an example graph which is Hamiltonian but not Eulerian.

The graph K4 has a Hamiltonian circuit A→B→C→D→A A → B → C → D → A which


runs around the outside of the above diagram. However, all four vertices of K4 have odd
degree. Since K4 has vertices of odd degree, K4 does not have an Eulerian circuit.

Qn:What is the difference between prims and kruskal’s algorithm for spanning trees?

S.No. Prim’s Algorithm Kruskal’s Algorithm

1 This algorithm begins to construct the This algorithm begins to construct the shortest
shortest spanning tree from any vertex in spanning tree from the vertex having the lowest
the graph. weight in the graph.

2 To obtain the minimum distance, it It crosses one node only one time.
traverses one node more than one time.

3 The time complexity of Prim’s algorithm The time complexity of Kruskal’s algorithm is O(E
is O(V2). log V).

4 In Prim’s algorithm, all the graph elements Kruskal’s algorithm may have disconnected graphs.
must be connected.

5 When it comes to dense graphs, the Prim’s When it comes to sparse graphs, Kruskal’s algorithm
algorithm runs faster. runs faster.
6 It prefers list data structure. It prefers the heap data structure.

10 a) Show that in any connected planar graph | V | - | E | + | R | = 2.

Take an example graph and show.

V=no of vertices

E= no of Edges

R=no of regions

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