Unit - 3
Unit - 3
Unit - 3
1
Q) 1. What is graph
A Graph consisting of a finite set of vertices(or nodes) and a set of edges that
connect a pair of nodes
Graph Data Structure is a collection of nodes connected by edges. It’s used to represent
relationships between different entities. Graph algorithms are methods used to
manipulate and analyze graphs, solving various problems like finding the shortest
path or detecting cycles.
A Graph is a non-linear data structure that consists of vertices (nodes) and edges.
A vertex, also called a node in the Graph, and an edge is used to connect two vertices
with each other.
Graphs are non-linear because the data structure allows us to have different paths to
get from one vertex to another, unlike with linear data structures like Arrays or Linked
Lists.
Graphs are used to represent and solve problems where the data consists of objects
and relationships between them, such as:
Social Networks: Each person is a vertex, and relationships (like friendships) are
the edges. Algorithms can suggest potential friends.
Maps and Navigation: Locations, like a town or bus stops, are stored as vertices,
and roads are stored as edges. Algorithms can find the shortest route between
two locations when stored as a Graph.
Internet: Can be represented as a Graph, with web pages as vertices and
hyperlinks as edges.
Biology: Graphs can model systems like neural networks or the spread of diseases.
1. Finite Graphs
A graph is said to be finite if it has a finite number of vertices and a finite number of
edges. A finite graph is a graph with a finite number of vertices and edges. In other
Unit - 3
3
words, both the number of vertices and the number of edges in a finite graph are
limited and can be counted. Finite graphs are often used to model real-world
situations, where there is a limited number of objects and relationships between the
2. Infinite Graph:
A graph is said to be infinite if it has an infinite number of vertices as well as an infinite
number of edges.
3. Trivial Graph:
A graph is said to be trivial if a finite graph contains only one vertex and no edge. A trivial
graph is a graph with only one vertex and no edges. It is also known as a singleton
graph or a single vertex graph. A trivial graph is the simplest type of graph and is
often used as a starting point for building more complex graphs. In graph theory,
trivial graphs are considered to be a degenerate case and are not typically studied in
detail
Unit - 3
4
4. Simple Graph:
A simple graph is a graph that does not contain more than one edge between the pair of
vertices. A simple railway track connecting different cities is an example of a simple
graph.
5. Multi Graph:
Any graph which contains some parallel edges but doesn’t contain any self-loop is called
a multigraph. For example a Road Map.
Parallel Edges: If two vertices are connected with more than one edge then such
edges are called parallel edges that are many routes but one destination.
Loop: An edge of a graph that starts from a vertex and ends at the same vertex is
called a loop or a self-loop.
Unit - 3
5
6. Null Graph:
A graph of order n and size zero is a graph where there are only isolated vertices with no
edges connecting any pair of vertices.A null graph is a graph with no edges. In other
words, it is a graph with only vertices and no connections between them. A null graph
can also be referred to as an edgeless graph, an isolated graph, or a discrete graph
7. Complete Graph:
A simple graph with n vertices is called a complete graph if the degree of each vertex is n-
1, that is, one vertex is attached with n-1 edges or the rest of the vertices in the
graph. A complete graph is also called Full Graph.
Unit - 3
6
8. Pseudo Graph:
A graph G with a self-loop and some multiple edges is called a pseudo graph. A
pseudograph is a type of graph that allows for the existence of self-loops (edges that
connect a vertex to itself) and multiple edges (more than one edge connecting two
vertices). In contrast, a simple graph is a graph that does not allow for loops or
multiple edges.
9. Regular Graph:
A simple graph is said to be regular if all vertices of graph G are of equal degree. All
complete graphs are regular but vice versa is not possible. A regular graph is a type of
undirected graph where every vertex has the same number of edges or neighbors. In
other words, if a graph is regular, then every vertex has the same degree.
Unit - 3
7
11. Subgraph:
A graph G1 = (V1, E1) is called a subgraph of a graph G(V, E) if V1(G) is a subset of V(G)
and E1(G) is a subset of E(G) such that each edge of G1 has same end vertices as in G.
Unit - 3
8
Advantages of graphs:
1. Graphs can be used to model and analyze complex systems and relationships.
2. They are useful for visualizing and understanding data.
3. Graph algorithms are widely used in computer science and other fields, such as
social network analysis, logistics, and transportation.
4. Graphs can be used to represent a wide range of data types, including social
networks, road networks, and the internet.
Disadvantages of graphs:
1. Large graphs can be difficult to visualize and analyze.
2. Graph algorithms can be computationally expensive, especially for large graphs.
3. The interpretation of graph results can be subjective and may require domain-
specific knowledge.
4. Graphs can be susceptible to noise and outliers, which can impact the accuracy of
analysis results.
Q) 3. Graph traversal :-
graph traversal का अर्थ है ग्राफ के प्रत्येक node को visit करना. यहाँ पर
हम दो प्रकार के traversal की बात करेंगे.
1:- BFS (breadth first search)
2:- DFS (depth first search)
BFS algorithm:-
स्टेप 2:– सबसे पहले हम node A (शुरूआती नोड) को विजिट करते है और इसे
visited मार्क करते है.
स्टेप 3:– इसके बाद हम A के adjacent nodes को देखते है. इसके adjacent
नोड्स B, C तथा D है. इस चित्र में हम सबसे पहले B को विजिट करते है और
उसे queue में रखते है.
Unit - 3
11
स्टेप 4:– इसके बाद हम नोड A के adjacent node C को विजिट करते है और उसे
queue में रखते है.
स्टेप 5:- इसके बाद A के अंतिम adjacent नोड D को विजिट करते है और उसे
queue में रखते है.
स्टेप 6:- इसके बाद में A के कोई adjacent नोड नहीं बचे इसलिए हम B को
queue से निकालते है और उसके adjacent को search करते है.
अब हमारे पास विजिट करने के लिए कोई भी नोड बही बचा है परन्तु हमें
सभी nodes को queue से निकालना होगा. और जब queue खाली हो जायेगा तो
प्रोग्राम समाप्त हो जायेगा.
DFS algorithm:-
DFS में हम सबसे पहले शुरूआती node को stack के द्वारा विजिट करते है.
फिर इसकी समस्त adjacent nodes को stack में डालकर, stack को top में स्थित
नोड को विजिट करके, उसके समस्त adjacent node को stack में डाल देते है और
प्रक्रिया तब तक दोहराते जब तक कि stack खाली नहीं हो जाता है.
इसकी अल्गोरिथम को निम्नलिखित उदाहरण के द्वारा समझते है.
स्टेप 2:– नोड A को visited मार्क करते है और उसे स्टैक में डालते हैं.
नोड A के तीन adjacent नोड है B, C, तथा D. हम इनमें से किसी भी नोड को चुन
सकते है. हम alphabetical क्रम में चुनेंगें.
Unit - 3
13
स्टेप 4:- E को विजिट करेंगे और इसे visited मार्क करेंगे और इसे stack में
डाल देंगे. यहाँ पर नोड E के दो adjacent नोड C तथा D है दोनों unvisited है
तो हम C को select करेंगे. (alphabetical क्रम में.)
अब विजिट करने के लिए कोई नोड नहीं बचा है अब हम सभी नोड्स को stack में
से बाहर निकालेंगे और जब stack empty हो जाएगा तो प्रोग्राम समाप्त हो
जाएगा.
Q) 4. Applications of Graph
In Computer science graphs are used to represent the flow of computation.
Google maps uses graphs for building transportation systems, where two(or more) roads are
considered to be a vertex and the road connecting two vertices is considered to be an edge,
thus their navigation system is based on the algorithm to calculate the shortest path
between two vertices.
In Facebook, users are considered to be the vertices and if they are friends then there is an
edge running between them. Facebook’s Friend suggestion algorithm uses graph theory.
Facebook is an example of undirected graph.
In World Wide Web, web pages are considered to be the vertices. There is an edge from a
page u to other page v if there is a link of page v on page u. This is an example of Directed
graph. It was the basic idea behind Google Page Ranking Algorithm.
Unit - 3
15
In Operating System, we come across the Resource Allocation Graph where each process
and resources are considered to be vertices. Edges are drawn from resources to the allocated
process, or from requesting process to the requested resource. If this leads to any formation
of a cycle then a deadlock will occur.
In mapping system we use graph. It is useful to find out which is an excellent place from the
location as well as your nearby location. In GPS we also use graphs.
Facebook uses graphs. Using graphs suggests mutual friends. it shows a list of the f following
pages, friends, and contact list.
Microsoft Excel uses DAG means Directed Acyclic Graphs.
In the Dijkstra algorithm, we use a graph. we find the smallest path between two or many
nodes.
On social media sites, we use graphs to track the data of the users. liked showing preferred
post suggestions, recommendations, etc.
Graphs are used in biochemical applications such as structuring of protein, DNA etc.
Thus the development of algorithms to handle graphs is of major interest in the field of
computer science.
Three Applications of Graphs in the area of computer engineering:
1. The applications of graph split broadly into three categories:
a) First, analysis to determine structural properties of a network, such as the distribution of
vertex degrees and the diameter of the graph. A vast number of graph measures exist.
b) Second, analysis to find a measurable quantity within the network, for example, for a
transportation network, the level of vehicular flow within any portion of it.
c) Third, analysis of dynamic properties of network. Map of a country can be represented using
a graph. Road network, Air network or rail network can be represented using a graph.
Connection among routers in a communication network can be represented using a graph.
Routing of a packet between two communicating nodes can be done through the shortest
path.
2. Graph theory is useful in biology and conservation efforts where a vertex can represent
regions where certain species exist and the edges represent migration paths, or movement
between the regions. This information is important when looking at breeding patterns or
tracking the spread of disease.
3. Different activities of a project can be represented using a graph. This graph can be useful in
project scheduling.
Unit - 3
16
Q) 5. Bellman–Ford Algorithm
Imagine you have a map with different cities connected by roads, each road having a certain
distance. The Bellman–Ford algorithm is like a guide that helps you find the shortest path
from one city to all other cities, even if some roads have negative lengths. It’s like a GPS for
computers, useful for figuring out the quickest way to get from one point to another in a
network.
Bellman-Ford is a single source shortest path algorithm that determines the shortest path
between a given source vertex and every other vertex in a graph. This algorithm can be used
on both weighted and unweighted graphs.
A Bellman-Ford algorithm is also guaranteed to find the shortest path in a graph, similar
to Dijkstra’s algorithm. Although Bellman-Ford is slower than Dijkstra’s algorithm, it is capable
of handling graphs with negative edge weights, which makes it more versatile.
The shortest path cannot be found if there exists a negative cycle in the graph. If we continue
to go around the negative cycle an infinite number of times, then the cost of the path will
continue to decrease .As a result, Bellman-Ford is also capable of detecting negative cycles,
which is an important feature.
Initial Graph
Step 1: Initialize a distance array Dist[] to store the shortest distance for each vertex from the
source vertex. Initially distance of source will be 0 and Distance of other vertices will be
INFINITY.
Unit - 3
17
1st Relaxation
Step 3: During 2nd Relaxation:
Current Distance of D > (Distance of B) + (Weight of B to D) i.e. Infinity > 5 + 2
o Dist[D] = 7
Current Distance of C > (Distance of B) + (Weight of B to C) i.e. Infinity > 5 + 1
o Dist[C] = 6
Unit - 3
18
2nd Relaxation
Step 4: During 3rd Relaxation:
Current Distance of F > (Distance of D ) + (Weight of D to F) i.e. Infinity > 7 + 2
o Dist[F] = 9
Current Distance of E > (Distance of C ) + (Weight of C to E) i.e. Infinity > 6 + 1
o Dist[E] = 7
3rd Relaxation
Step 5: During 4th Relaxation:
Unit - 3
19
Current Distance of D > (Distance of E) + (Weight of E to D) i.e. 7 > 7 + (-1)
o Dist[D] = 6
Current Distance of E > (Distance of F ) + (Weight of F to E) i.e. 7 > 9 + (-3)
o Dist[E] = 6
4th Relaxation
Step 6: During 5th Relaxation:
Current Distance of F > (Distance of D) + (Weight of D to F) i.e. 9 > 6 + 2
o Dist[F] = 8
Current Distance of D > (Distance of E ) + (Weight of E to D) i.e. 6 > 6 + (-1)
o Dist[D] = 5
Since the graph h 6 vertices, So during the 5th relaxation the shortest distance for all the
vertices should have been calculated.
Unit - 3
20
5th Relaxation
Step 7: Now the final relaxation i.e. the 6th relaxation should indicate the presence of negative
cycle if there is any changes in the distance array of 5th relaxation.
During the 6th relaxation, following changes can be seen:
Current Distance of E > (Distance of F) + (Weight of F to E) i.e. 6 > 8 + (-3)
o Dist[E]=5
Current Distance of F > (Distance of D ) + (Weight of D to F) i.e. 8 > 5 + 2
o Dist[F]=7
Since, we observer changes in the Distance array Hence ,we can conclude the presence of a
negative cycle in the graph.
6th Relaxation
Result: A negative cycle (D->F->E) exists in the graph.
Q) 7. Spanning tree
A spanning tree is a subset of Graph G, which has all the vertices covered with minimum
possible number of edges. Hence, a spanning tree does not have cycles and it cannot be
disconnected.
Unit - 3
22
A spanning tree can be created from a graph in many ways. Here in the above
picture, three types of spanning trees have been created from a graph. These
trees contain all the vertices of the graph and some edges. Keep in mind that no
spanning tree should create a cycle.
Cost of spanning tree :-
The cost of a spanning tree can be calculated by adding up the weights of its
vertices.
Unit - 3
23
As :-