Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit - 3

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 26

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.

Types of data structure graph


Unit - 3
2
1. Undirected Graphs: A graph in which edges have no direction, i.e., the edges do not
have arrows indicating the direction of traversal.
Example: A social network graph where friendships are not directional.
2. Directed Graphs: A graph in which edges have a direction, i.e., the edges have arrows
indicating the direction of traversal.
Example: A web page graph where links between pages are directional.
3. Weighted Graphs: A graph in which edges have weights with them.
Example: A road network graph where the weights can represent the distance
between two cities.
4. Unweighted Graphs: A graph in which edges have no weights with them.
Example: A social network graph where the edges represent friendships.
5. Complete Graphs: A graph in which each vertex is connected to every other vertex.
Example: A tournament graph where every player plays against every other player.
6. Bipartite Graphs: A graph in which the vertices can be divided into two disjoint sets
such that every edge connects a vertex in one set to a vertex in the other set.
Example: A job applicant graph where the vertices can be divided into job applicants
and job openings.
7. Trees: A connected graph with no cycles.
Example: A family tree where each person is connected to their parents.
8. Cycles: A graph with at least one cycle.
Example: A bike-sharing graph where the cycles represent the routes that the bikes
take.
9. Sparse Graphs: A graph with relatively few edges compared to the number of
vertices.
Example: A chemical reaction graph where each vertex represents a chemical
compound and each edge represents a reaction between two compounds.
10. Dense Graphs: A graph with many edges compared to the number of vertices.
Example: A social network graph where each vertex represents a person and each
edge represents a friendship.
Types of Graphs:

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

10. Bipartite Graph:


A graph G = (V, E) is said to be a bipartite graph if its vertex set V(G) can be partitioned
into two non-empty disjoint subsets. V1(G) and V2(G) in such a way that each edge e
of E(G) has one end in V1(G) and another end in V2(G). The partition V1 U V2 = V is
called Bipartite of G. Here in the figure: V1(G)={V5, V4, V3} and V2(G)={V1, V2}

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) 2. What is Directed Acyclic Graph?


A Directed Acyclic Graph (DAG) is a directed graph that does not contain any cycles.
Unit - 3
9
Direct Acyclic Graph

Meaning of Directed Acyclic Graph:


Directed Acyclic Graph has two important features:
 DirectedEdges: In Directed Acyclic Graph, each edge has a direction, meaning it goes from
one vertex (node) to another. This direction signifies a one-way relationship or dependency
between nodes.
 Acyclic:
The term “acyclic” indicates that there are no cycles or closed loops within the
graph. In other words, you cannot traverse a sequence of directed edges and return to the
same node, following the edge directions. Formation of cycles is prohibited in DAG. Hence
this characteristic is essential.

Directed Acyclic Graph

Q) 3. Graph traversal :-
graph traversal का अर्थ है ग्राफ के प्रत्येक node को visit करना. यहाँ पर
हम दो प्रकार के traversal की बात करेंगे.
1:- BFS (breadth first search)
2:- DFS (depth first search)

1:- BFS (breadth first search):-


BFS ग्राफ डेटा स्ट्रक्चर को travers तथा search करने की एक अल्गोरिथम
है.
इसका प्रयोग ग्राफ में shortest path को ढूँढने तथा puzzle गेम्स को solve
करने के लिए किया जाता है.
डेटा स्ट्रक्चर में, BFS को implement करने के लिए queue का प्रयोग किया
जाता है.
Unit - 3
10
BFS में nodes को breadth wise visit किया जाता है.
BFS में पहले किसी भी एक node को visit किया जाता है तथा उसके बाद उसके
नजदीक के नोड्स को visit किया जाता है. इसके बाद इन adjacent नोड के भी सभी
adjacent node को विजिट किया जाता है. और यह प्रक्रिया तब तक चलती है जब
तक कि सभी nodes को विजिट नहीं कर लिया जाता है.

BFS algorithm:-

स्टेप 1:- queue को initialize किया जाता है.

स्टेप 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 करते है.

स्टेप 7:- नोड B का adjacent नोड E है तो हम E को विजिट करते है और उसे


queue में रखते है.

अब हमारे पास विजिट करने के लिए कोई भी नोड बही बचा है परन्तु हमें
सभी nodes को queue से निकालना होगा. और जब queue खाली हो जायेगा तो
प्रोग्राम समाप्त हो जायेगा.

2:- DFS (depth first search)


Unit - 3
12
DFS भी BFS की तरह ग्राफ डेटा स्ट्रक्चर को travers तथा search करने की एक
अल्गोरिथम है.
BFS में नोड्स को depth wise (गहराई से) विजिट किया जाता है.
डेटा स्ट्रक्चर में DFS को implement करने के लिए stack का प्रयोग किया
जाता है.
DFS एक recursive अल्गोरिथम है जो कि backtracking के सिधांत पर कार्य करती
है.
नेटवर्कों को analyze करने, routes को map करने तथा अन्य कंप्यूटर
विज्ञानं की परेशानियों को solve करने के लिए DFS का प्रयोग किया जाता
है.

DFS algorithm:-

DFS में हम सबसे पहले शुरूआती node को stack के द्वारा विजिट करते है.
फिर इसकी समस्त adjacent nodes को stack में डालकर, stack को top में स्थित
नोड को विजिट करके, उसके समस्त adjacent node को stack में डाल देते है और
प्रक्रिया तब तक दोहराते जब तक कि stack खाली नहीं हो जाता है.
इसकी अल्गोरिथम को निम्नलिखित उदाहरण के द्वारा समझते है.

स्टेप 1:– stack को intialize किया जाता है.

स्टेप 2:– नोड A को visited मार्क करते है और उसे स्टैक में डालते हैं.
नोड A के तीन adjacent नोड है B, C, तथा D. हम इनमें से किसी भी नोड को चुन
सकते है. हम alphabetical क्रम में चुनेंगें.
Unit - 3
13

स्टेप 3:- नोड B को visited मार्क करेंगे और स्टैक में डालेंगे. अब B के


किसी unvisited adjacent नोड्स को select करेंगे. B के adjacent नोड्स A तथा E
है चूँकि A को पहले ही विजिट कर लिया है तो हम E को select करेंगे.

स्टेप 4:- E को विजिट करेंगे और इसे visited मार्क करेंगे और इसे stack में
डाल देंगे. यहाँ पर नोड E के दो adjacent नोड C तथा D है दोनों unvisited है
तो हम C को select करेंगे. (alphabetical क्रम में.)

स्टेप 5:- हम c को विजिट करेंगे और इसे visited मार्क करेंगे और स्टैक


में रख देंगे. यहाँ पर B का कोई unvisited adjacent नोड नहीं है तो इसे stack
से निकाल देंगे.
Unit - 3
14
स्टेप 6:- अब stack के top पर वापस E है अब देखेंगे कि इसका कोई unvisited
adjacent node है या नही. इसका adjacent unvisited नोड D है.

स्टेप 7:- अब हम नोड D को विजिट करेंगे और इसे visited मार्क करेंगे और


इसे stack में डाल देंगे.

अब विजिट करने के लिए कोई नोड नहीं बचा है अब हम सभी नोड्स को 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.

Working of Bellman-Ford Algorithm to Detect the Negative cycle in the graph:

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

Initialize a distance array


Step 2: Start relaxing the edges, during 1st Relaxation:
 Current Distance of B > (Distance of A) + (Weight of A to B) i.e. Infinity > 0 + 5
o Therefore, Dist[B] = 5

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) 6. Floyd Warshall Algorithm


The Floyd Warshall Algorithm is an all pair shortest path algorithm
unlike Dijkstra and Bellman Ford which are single source shortest path algorithms. This
algorithm works for both the directed and undirected weighted graphs. But, it does not
Unit - 3
21
work for the graphs with negative cycles (where the sum of the edges in a cycle is
negative). It follows Dynamic Programming approach to check every possible path.
Floyd-Warshall अल्गोरिथ्म एक weighted graph में vertices के सभी pairs के
मध्य shortest path खोजने की एक algorithm है.
यह अल्गोरिथ्म directed और undirected दोनों graphs के लिए काम करती है. इस
algorithm को Floyd’s algorithm, Roy-Floyd algorithm, Roy-Warshall algorithm, या
WFI algorithm भी कहते हैं.
यह अल्गोरिथ्म shortest path को ढूंडने के लिए dynamic
programming एप्रोच को follow करता है.
The Floyd-Warshall algorithm is a classic algorithm used to find the shortest
paths between all pairs of vertices in a weighted graph. It can handle both
directed and undirected graphs and works even with graphs containing negative
weight edges, provided there are no negative weight cycles.

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

properties of spanning tree


1:- connected graph की एक से ज्यादा spanning tree हो सकती है.
2:- spanning tree में cycle नही होती है.
3:- अगर हम spanning tree में से एक edge को हटा देंगे तो graph disconnected
हो जायेगा.
4:- अगर हम spanning tree में एक edge को add कर देंगे तो वह loop या circuit
बना लेगा.

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

Minimum spanning tree


The minimum spanning tree is a spanning tree. The spanning tree whose cost is
the lowest is called the minimum spanning tree.
For example: Here a spanning tree is created from a graph whose joining of
vertices gives the lowest cost.

minimum spanning tree को find करने के लिए दो प्रकार के algorithm है –


1:- Prim’s algorithm
2:- Kruskal’s algorithm
1.Prim’s algorithm
prim’s algorithm एक greedy algorithm है जिसके द्वारा एक minimum spanning
tree find करते है। मतलब एक tree जिसमे सभी edges शामिल होंगे उसके edges
का एक subset find करेंगे। prim’s algorithm shortest first path के साथ समानता
share करता है।
यह algorithm एक single tree को nodes के जैसे treat करता है और दिए गए graph
से spanning tree बनाने के लिए उस पर new node जोड़ता है।
Prim’s algorithm में minimum spanning tree निर्मित करने के लिए सबसे
पहले एक vertex से शुरू करेंगे यह कोई भी arbitrary vertex हो सकता है हम
कही से भी spanning tree निर्मित करना शुरू कर सकते है। एक vertex को
choose करने के बाद उस vertex से जुड़े हुए सभी vertex को देखेंगे कि कौन
Unit - 3
24
से vertex का cost सबसे कम है। जिस vertex का cost सबसे कम होगा उस vertex को
choose करेंगे और पहले वाले vertex के साथ जोड़ देंगे। यह प्रकिया तब तक
चलते रहेगी जब तक सारे vertex ना जुड़ जाये और उसके साथ साथ यह भी ध्यान
में रखेंगे कि एक भी cycle ना निर्मित हो।
Algorithm :-
1:- Remove all the loops and all the parallel edges.
( सभी loops और parallel edges को अलग कर देंगे )
2:- Choose any arbitrary node as root node.
( एक node को root node के रूप में चुनेंगे यह node कोई सा भी हो सकता है
हम कही से भी spanning tree बनाना शुरू कर सकते है। )
3:- Check outgoing edges and select the one with less cost.
( Outgoing edges को check करेंगे और उन edges में से एक edge को select
करेंगे जिसका cost सबसे कम होगा। )
Ex :-
Unit - 3
25

Kruskal’s algorithm in hindi


kruskal’s algorithm एक greedy approach का उपयोग करके minimum spanning tree
को find करता है। यह minimum spanning tree को सभी properties को follow करता
है।
इस algorithm में graph को एक forest की तरह और सभी nodes को एक individual
tree की तरह treat किया जाता है।
Unit - 3
26
Kruskal algorithm की मदद से minimum spanning tree find करने के लिए सबसे
पहले एक सबसे कम cost वाला edge choose करेंगे उसके बाद फिर से सबसे कम
cost वाला edge choose करेंगे यह प्रक्रिया तब तक चलती रहेगी जब तक की
सभी vertex ना जुड़ जाये और साथ साथ एक cycle भी ना निर्मित हो। इसमें
हमेशा सबसे कम cost वाला ही edge choose करेंगे फिर चाहे एक vertex दुसरे
vertex से ना जुड़े।
Algorithm :-
1:- Remove all the loops and all parallel edges.
( सभी loops और parallel edges को अलग कर देंगे )
2:- Arrange all edges in their increasing order of weight.
( सभी edges को उनके बढ़ते क्रम के weight में arrange करेंगे। )
3:- Add all edges which has the least weightage.
( सभी edges को जोड़ेंगे जिनका महत्त्व या cost सबसे कम है। )
उदाहरण:-

You might also like