SlideShare ist ein Scribd-Unternehmen logo
1 von 43
Graph
1
By:
Dabal Singh Mahara
2017
Unit – 9 Graph
Contents Hours Marks
a) Introduction
b) Representation of Graph
• Array
• Linked list
c) Traversals
• Depth first Search
• Breadth first search
d) Minimum spanning tree
• Kruskal's algorithm
4 5
2
Introduction
• Graphs are a formalism for representing
relationships between objects.
– a graph G is represented as G = (V, E),
where,
• V is a set of vertices
• E is a set of edges
Balkhu
kalanki
Bafal
V = {Balkhu, Kalanki, Bafal}
E = {(Bafal, Kalanki),
(Balkhu, Kalanki),
(Kalanki, Balkhu)}
3
Graph
• A graph G = (V,E) is composed of:
V: set of vertices
E: set of edges connecting the vertices in V
• An edge e = (u,v) is a pair of vertices
• Example:
a b
c
d e
V= {a,b,c,d,e}
E= {(a,b),(a,c),(a,d),
(b,e),(c,d),(c,e),
(d,e)}
4
5
Graph ADT
Graph Terminology:
• Node
Each element of a graph is called node of a graph
• Edge
Line joining two nodes is called an edge.
It is denoted by e=[u,v] where u and v are adjacent vertices.
6
 Loop
An edge of the form (u, u) is said to be a loop. Here in figure [v2,v2]
 Multiedge
If x was y’s friend several times over, we can model this relationship
using multiedges. In figure e1 and e3.
Loop
e1
V1
V2e2V2
node
edge
e3
Adjacent and Incident
• If (v0, v1) is an edge in an undirected graph,
• v0 and v1 are adjacent
• The edge (v0, v1) is incident on vertices v0 and v1
• If <v0, v1> is an edge in a directed graph
• v0 is adjacent to v1, and v1 is adjacent from v0
• The edge <v0, v1> is incident on v0 and v1
7
• The degree of a vertex is the number of edges incident to that
vertex
• A node with degree 0 is known as isolated node.
• A node with degree 1 is known as pendant node.
• For directed graph,
• the in-degree of a vertex v is the number of edges
that have v as the head
• the out-degree of a vertex v is the number of edges
that have v as the tail
• if di is the degree of a vertex i in a graph G with n vertices and e
edges, the number of edges is
Degree of a Vertex
8
0
1 2
3 4 5 6
G1 G2
3
2
3 3
1 1 1 1
directed graph
in-degree
out-degree
0
1
2
G3
in:1, out: 1
in: 1, out: 2
in: 1, out: 0
0
1 2
3
33
3
Examples
9
10
Path
• Path: A sequence of
vertices v1,v2,. . .vk such
that consecutive vertices vi
and vi+1 are adjacent.
• Example: {1, 4, 3, 5, 2}
1
4 5
3
2
a b
c
d e
a b
c
d e
a b e d c b e d c
• Length of a path: Number of edges on the path
• simple path: The path with no repeated vertices
• cycle: simple path, except that the last vertex is the same as the
first vertex
a b
c
d e
b e c
11
• subgraph: subset of vertices and edges forming a graph
• connected component: maximal connected subgraph. E.g., the graph below has
3 connected components.
connected not connected
•connected graph: any two vertices are connected by some path
12
Types
• Graphs are generally classified as,
• Directed graph
• Undirected graph
13
Un Directed graph
• A graphs G is called directed graph if each edge has no
direction.
14
15
• Simple Graph
A graph in which there is no loop and no multiple edges between two nodes.
• Multigraph
The graph which has multiple edges between any two nodes but no loops is called
multigraph.
Types of Graph
Directed graph
• A graphs G is called directed graph if each edge has a
direction.
16
• Pseudograph
A graph which has loop is called pseudograph.
• Complete graph
A graph G is called complete, if every nodes are adjacent
with other node
• Weighted graph
If each edge of graph is assigned a number or value, then it
is weighted graph. The number is weight.
17
18
Why Use Graphs?
• Graphs serve as models of a wide range of objects:
– A roadmap
– A map of airline routes
– A layout of an adventure game world
– A schematic of the computers and connections that make up the Internet
– The links between pages on the Web
– The relationship between students and courses
– A diagram of the flow capacities in a communications or transportation network
19
Representations of Graphs
• To represent graphs, you need a convenient way to store the
vertices and the edges that connect them
• Two commonly used representations of graphs:
– The adjacency matrix
– The adjacency list
20
Adjacency Matrix
• If a graph has N vertices labeled 0, 1, . . . , N – 1:
– The adjacency matrix for the graph is a grid G with N rows and N columns
– Cell G[i][ j] = 1 if there’s an edge from vertex i to j
• Otherwise, there is no edge and that cell contains 0
• These are the simplest ways for representing graphs.
• Space requirement: O(n2)
• Adding and deleting edge: O(1)
• Testing an edge : O(1)
21
Adjacency Matrix (continued)
• If the graph is undirected, then four more cells are occupied by 1:
• If the vertices are labeled, then the labels can be stored in a
separate one-dimensional array
22
Adjacency List
• If a graph has N vertices labeled 0, 1, . . . , N – 1,
– The adjacency list for the graph is an array of N linked lists
– The ith linked list contains a node for vertex j if and only if there is an
edge from vertex i to vertex j
– It is suitable for sparse graphs i.e. graphs with the few edges
– Space required: O(V+E)
– Time for
• Testing edge to u O(dge(u))
• Finding adjacent vertices: O(deg(u))
• Insertion and deletion : O(deg(u))
23
Adjacency List (continued)
24
25
Exercise:
Construct Adjacency List and Adjacency Matrix
i. ii.
1
5 4
3
2
26
 One of the most fundamental graph problems is to traverse every edge and
vertex in a graph. Applications include:
 Printing out the contents of each edge and vertex.
 Counting the number of edges.
 Identifying connected components of a graph.
 Graph traversal algorithms visit the vertices of a graph, according to some
Strategy.
 Given G=(V,E) and vertex v, find all wV, such that w connects v
 Depth First Search (DFS): preorder traversal
 Breadth First Search (BFS): level order traversal
Graph Traversals
27
DFS
The basic idea is:
• Start from the given vertex and go as far as possible
i.e. search continues until the end of the path if not visited
already,
• Otherwise, backtrack and try another path.
• DFS uses stack to process the nodes.
Algorithm:
DFS(G,S)
{
T = { S };
Traverse(S);
}
Traverse(v)
{
for each w adjacent to v not yet in T
{
T = T U {w}; // add edge {v,w} in T
Traverse(w);
}
}
28
2
1
6
7
5
3
4
Example: DFS Tracing
Starting Vertex : 1
2
1
6
7
5
3
4
Visited Nodes: T = { }
2
1
6
7
5
3
4
T = { 1 }
T = { 1, 2 }
29
2
1
6
7
5
3
4
T = { 1, 2, 3 }
backtrack from 3
Visit next branch from 2
2
1
6
7
5
3
4
T = { 1, 2, 3, 4 }
2
1
6
7
5
3
4
T = { 1, 2, 3, 4, 7 }
30
T = { 1, 2, 3, 4, 7, 5 }
2
1
6
7
5
3
4
2
1
6
7
5
3
4
T = { 1, 2, 3, 4, 7, 5, 6 }
2
1
6
7
5
3
4
Final DFS tree.
31
Analysis:
• The complexity of the algorithm is greatly affected by
Traverse function we can write its running time in terms of
the relation,
T(n) = T(n-1) + O(n),
• At each recursive call a vertex is decreased and for each
vertex atmost n adjacent vertices can be there. So, O(n).
• Solving this we get, T(n) = O(n2). This is the case when
we use adjacency matrix.
• If adjacency list is used, T(n) = O(n + e), where e is
number of edges.
32
BFS
• This is one of the simplest methods of graph searching.
• Choose some vertex as a root or starting vertex.
• Add it to the queue. Mark it as visited and dequeue it from the queue.
• Add all the adjacent vertices of this node into queue.
• Remove one node from front and mark it as visited. Repeat this process until all the
nodes are visited.
BFS (G, S)
{
Initialize Queue, q = { }
mark S as visited;
enqueue(q, S);
while( q != Empty)
{
v = dequeue(q);
for each w adjacent to v
{
if w is not marked as visited
{
enqueue(q, w)
mark w as visited.
}
}
}
}
33
Analysis:
• This algorithms puts all the vertices in the queue and they
are accessed one by one.
• for each accessed vertex from the queue their adjacent
vertices are looked up for O(n) time (for worst case).
• Total time complexity , T(n) = O(n2) , in case of adjacency
matrix.
• T(n) = O(n +e), in case of adjacency list.
34
2
1
6
7
5
3
4
Example: BFS Algorithm Tracing
Solution:
2
1
6
7
5
3
4
 Starting vertex : 1
Visited: { 1 }
2
1
6
7
5
3
4
Visited: { 1,2, 3, 7, 5, 6 }
1
2 3 7 5 6Queue :
Queue :
35
2
1
6
7
5
3
4
 Dequeue front of queue. Add its all unvisited
adjacent nodes to the queue.
Queue 3 7 5 6 4
Visited : { 1, 2, 3, 7, 5, 6, 4 }
2
1
6
7
5
3
4
Queue
Visited : { 1, 2, 3, 7, 5, 6, 4 }
36
2
1
6
7
5
3
4
Final BFS Tree.
37
Spanning Tree
A spanning tree of a connected undirected graph G is a sub graph
T of without cycle G that connects all the vertices of G.
i.e. A spanning tree for a connected graph G is a tree containing
all the vertices of G
• A minimum spanning tree in a connected weighted graph is a
spanning tree that has the smallest possible sum of weights of its
edges.
• It represents the cheapest way of connecting all the nodes in G.
• It is not necessarily unique.
• Any time you want to visit all vertices in a graph at minimum cost
(e.g., wire routing on printed circuit boards, sewer pipe layout,
road planning…)
Minimum Spanning Trees
38
• Two algorithms that are used to construct the minimum
spanning tree from the given connected weighted graph of
given graph are:
 Kruskal's Algorithm
 Prim's Algorithm
MST Generating Algorithms
Kruskal's Algorithm
We have V as a set of n vertices and E as set of edges of graph G. The idea behind
this algorithm is:
• The nodes of the graph are considered as n distinct partial trees with one
node each.
• At each step of the algorithm, two partial trees are connected into single
partial tree by an edge of the graph.
• While connecting two nodes of partial trees, minimum weighted arc is
selected.
• After n-1 steps MST is obtained.
39
40
41
Algorithm:
Kruskal_MSt( G )
{
T = { V } // forest of n nodes
S = Set of edges sorted in non-decreasing order of weight
while( |T| < n-1 AND S != Empty)
{
select edge (u,v) from S in order
S = S – (u,v)
if (u,v) does not form cycle in T
T = T U {(u,v)}
}
}
Complexity Analysis
To form the forest of n trees takes O(n) time, the creation of S takes O(E.log E)
time and while loop executes O(n) time and the steps inside loop take almost
linear time.
So, total time – O(n) + O (E logE) +O(n log n)
42
2
1
6
7
5
3
4
18
7
10
15
8
9
11
13
7
Exercise: Trace Kruskals algorithm.
43

Weitere ähnliche Inhalte

Was ist angesagt?

Adjacency list
Adjacency listAdjacency list
Adjacency listStefi Yu
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsYi-Lung Tsai
 
Graph representation
Graph representationGraph representation
Graph representationTech_MX
 
14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpathSSE_AndyLi
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringSaurabh Kaushik
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphsKumar
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structurePooja Bhojwani
 
Attributed Graph Matching of Planar Graphs
Attributed Graph Matching of Planar GraphsAttributed Graph Matching of Planar Graphs
Attributed Graph Matching of Planar GraphsRaül Arlàndez
 
Graph terminologies & special type graphs
Graph terminologies & special type graphsGraph terminologies & special type graphs
Graph terminologies & special type graphsNabeel Ahsen
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data StructureAnuj Modi
 

Was ist angesagt? (20)

Adjacency list
Adjacency listAdjacency list
Adjacency list
 
Graphs Algorithms
Graphs AlgorithmsGraphs Algorithms
Graphs Algorithms
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
 
Graph representation
Graph representationGraph representation
Graph representation
 
Graphs
GraphsGraphs
Graphs
 
14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath14 chapter9 graph_algorithmstopologicalsort_shortestpath
14 chapter9 graph_algorithmstopologicalsort_shortestpath
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
 
1535 graph algorithms
1535 graph algorithms1535 graph algorithms
1535 graph algorithms
 
Graph
GraphGraph
Graph
 
Graphs in Data Structure
Graphs in Data StructureGraphs in Data Structure
Graphs in Data Structure
 
Topological Sort
Topological SortTopological Sort
Topological Sort
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Lecture25
Lecture25Lecture25
Lecture25
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Attributed Graph Matching of Planar Graphs
Attributed Graph Matching of Planar GraphsAttributed Graph Matching of Planar Graphs
Attributed Graph Matching of Planar Graphs
 
Graph terminologies & special type graphs
Graph terminologies & special type graphsGraph terminologies & special type graphs
Graph terminologies & special type graphs
 
6. Graphs
6. Graphs6. Graphs
6. Graphs
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data Structure
 

Ähnlich wie Unit 9 graph

Ähnlich wie Unit 9 graph (20)

Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
 
Graph 1
Graph 1Graph 1
Graph 1
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
10.graph
10.graph10.graph
10.graph
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
 
Unit V - ppt.pptx
Unit V - ppt.pptxUnit V - ppt.pptx
Unit V - ppt.pptx
 
UNIT III.pptx
UNIT III.pptxUNIT III.pptx
UNIT III.pptx
 
Graphs
GraphsGraphs
Graphs
 
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
141222 graphulo ingraphblas
141222 graphulo ingraphblas141222 graphulo ingraphblas
141222 graphulo ingraphblas
 
141205 graphulo ingraphblas
141205 graphulo ingraphblas141205 graphulo ingraphblas
141205 graphulo ingraphblas
 
lecture11.ppt
lecture11.pptlecture11.ppt
lecture11.ppt
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
 
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on GraphsCS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
 
Ppt 1
Ppt 1Ppt 1
Ppt 1
 

Mehr von Dabbal Singh Mahara (20)

Temporal databases
Temporal databasesTemporal databases
Temporal databases
 
Spatial databases
Spatial databasesSpatial databases
Spatial databases
 
Ordbms
OrdbmsOrdbms
Ordbms
 
Odbms concepts
Odbms conceptsOdbms concepts
Odbms concepts
 
Object database standards, languages and design
Object database standards, languages and designObject database standards, languages and design
Object database standards, languages and design
 
Normalization
NormalizationNormalization
Normalization
 
Mobile databases
Mobile databasesMobile databases
Mobile databases
 
Active database
Active databaseActive database
Active database
 
Deductive databases
Deductive databasesDeductive databases
Deductive databases
 
Relational model
Relational modelRelational model
Relational model
 
Overview of dbms
Overview of dbmsOverview of dbms
Overview of dbms
 
ER modeling
ER modelingER modeling
ER modeling
 
EER modeling
EER modelingEER modeling
EER modeling
 
Unit 7 sorting
Unit   7 sortingUnit   7 sorting
Unit 7 sorting
 
Unit 6 tree
Unit   6 treeUnit   6 tree
Unit 6 tree
 
Unit 5 linked list
Unit   5 linked listUnit   5 linked list
Unit 5 linked list
 
Unit 4 queue
Unit   4 queueUnit   4 queue
Unit 4 queue
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
 
Unit 3 stack
Unit   3 stackUnit   3 stack
Unit 3 stack
 
Unit 2 algorithm
Unit   2 algorithmUnit   2 algorithm
Unit 2 algorithm
 

Kürzlich hochgeladen

KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Christo Ananth
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).pptssuser5c9d4b1
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSRajkumarAkumalla
 

Kürzlich hochgeladen (20)

KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
247267395-1-Symmetric-and-distributed-shared-memory-architectures-ppt (1).ppt
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICSHARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
HARDNESS, FRACTURE TOUGHNESS AND STRENGTH OF CERAMICS
 

Unit 9 graph

  • 2. Unit – 9 Graph Contents Hours Marks a) Introduction b) Representation of Graph • Array • Linked list c) Traversals • Depth first Search • Breadth first search d) Minimum spanning tree • Kruskal's algorithm 4 5 2
  • 3. Introduction • Graphs are a formalism for representing relationships between objects. – a graph G is represented as G = (V, E), where, • V is a set of vertices • E is a set of edges Balkhu kalanki Bafal V = {Balkhu, Kalanki, Bafal} E = {(Bafal, Kalanki), (Balkhu, Kalanki), (Kalanki, Balkhu)} 3
  • 4. Graph • A graph G = (V,E) is composed of: V: set of vertices E: set of edges connecting the vertices in V • An edge e = (u,v) is a pair of vertices • Example: a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d), (b,e),(c,d),(c,e), (d,e)} 4
  • 6. Graph Terminology: • Node Each element of a graph is called node of a graph • Edge Line joining two nodes is called an edge. It is denoted by e=[u,v] where u and v are adjacent vertices. 6  Loop An edge of the form (u, u) is said to be a loop. Here in figure [v2,v2]  Multiedge If x was y’s friend several times over, we can model this relationship using multiedges. In figure e1 and e3. Loop e1 V1 V2e2V2 node edge e3
  • 7. Adjacent and Incident • If (v0, v1) is an edge in an undirected graph, • v0 and v1 are adjacent • The edge (v0, v1) is incident on vertices v0 and v1 • If <v0, v1> is an edge in a directed graph • v0 is adjacent to v1, and v1 is adjacent from v0 • The edge <v0, v1> is incident on v0 and v1 7
  • 8. • The degree of a vertex is the number of edges incident to that vertex • A node with degree 0 is known as isolated node. • A node with degree 1 is known as pendant node. • For directed graph, • the in-degree of a vertex v is the number of edges that have v as the head • the out-degree of a vertex v is the number of edges that have v as the tail • if di is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges is Degree of a Vertex 8
  • 9. 0 1 2 3 4 5 6 G1 G2 3 2 3 3 1 1 1 1 directed graph in-degree out-degree 0 1 2 G3 in:1, out: 1 in: 1, out: 2 in: 1, out: 0 0 1 2 3 33 3 Examples 9
  • 10. 10 Path • Path: A sequence of vertices v1,v2,. . .vk such that consecutive vertices vi and vi+1 are adjacent. • Example: {1, 4, 3, 5, 2} 1 4 5 3 2 a b c d e a b c d e a b e d c b e d c • Length of a path: Number of edges on the path
  • 11. • simple path: The path with no repeated vertices • cycle: simple path, except that the last vertex is the same as the first vertex a b c d e b e c 11
  • 12. • subgraph: subset of vertices and edges forming a graph • connected component: maximal connected subgraph. E.g., the graph below has 3 connected components. connected not connected •connected graph: any two vertices are connected by some path 12
  • 13. Types • Graphs are generally classified as, • Directed graph • Undirected graph 13
  • 14. Un Directed graph • A graphs G is called directed graph if each edge has no direction. 14
  • 15. 15 • Simple Graph A graph in which there is no loop and no multiple edges between two nodes. • Multigraph The graph which has multiple edges between any two nodes but no loops is called multigraph. Types of Graph
  • 16. Directed graph • A graphs G is called directed graph if each edge has a direction. 16 • Pseudograph A graph which has loop is called pseudograph.
  • 17. • Complete graph A graph G is called complete, if every nodes are adjacent with other node • Weighted graph If each edge of graph is assigned a number or value, then it is weighted graph. The number is weight. 17
  • 18. 18 Why Use Graphs? • Graphs serve as models of a wide range of objects: – A roadmap – A map of airline routes – A layout of an adventure game world – A schematic of the computers and connections that make up the Internet – The links between pages on the Web – The relationship between students and courses – A diagram of the flow capacities in a communications or transportation network
  • 19. 19 Representations of Graphs • To represent graphs, you need a convenient way to store the vertices and the edges that connect them • Two commonly used representations of graphs: – The adjacency matrix – The adjacency list
  • 20. 20 Adjacency Matrix • If a graph has N vertices labeled 0, 1, . . . , N – 1: – The adjacency matrix for the graph is a grid G with N rows and N columns – Cell G[i][ j] = 1 if there’s an edge from vertex i to j • Otherwise, there is no edge and that cell contains 0 • These are the simplest ways for representing graphs. • Space requirement: O(n2) • Adding and deleting edge: O(1) • Testing an edge : O(1)
  • 21. 21 Adjacency Matrix (continued) • If the graph is undirected, then four more cells are occupied by 1: • If the vertices are labeled, then the labels can be stored in a separate one-dimensional array
  • 22. 22 Adjacency List • If a graph has N vertices labeled 0, 1, . . . , N – 1, – The adjacency list for the graph is an array of N linked lists – The ith linked list contains a node for vertex j if and only if there is an edge from vertex i to vertex j – It is suitable for sparse graphs i.e. graphs with the few edges – Space required: O(V+E) – Time for • Testing edge to u O(dge(u)) • Finding adjacent vertices: O(deg(u)) • Insertion and deletion : O(deg(u))
  • 24. 24
  • 25. 25 Exercise: Construct Adjacency List and Adjacency Matrix i. ii. 1 5 4 3 2
  • 26. 26  One of the most fundamental graph problems is to traverse every edge and vertex in a graph. Applications include:  Printing out the contents of each edge and vertex.  Counting the number of edges.  Identifying connected components of a graph.  Graph traversal algorithms visit the vertices of a graph, according to some Strategy.  Given G=(V,E) and vertex v, find all wV, such that w connects v  Depth First Search (DFS): preorder traversal  Breadth First Search (BFS): level order traversal Graph Traversals
  • 27. 27 DFS The basic idea is: • Start from the given vertex and go as far as possible i.e. search continues until the end of the path if not visited already, • Otherwise, backtrack and try another path. • DFS uses stack to process the nodes. Algorithm: DFS(G,S) { T = { S }; Traverse(S); } Traverse(v) { for each w adjacent to v not yet in T { T = T U {w}; // add edge {v,w} in T Traverse(w); } }
  • 28. 28 2 1 6 7 5 3 4 Example: DFS Tracing Starting Vertex : 1 2 1 6 7 5 3 4 Visited Nodes: T = { } 2 1 6 7 5 3 4 T = { 1 } T = { 1, 2 }
  • 29. 29 2 1 6 7 5 3 4 T = { 1, 2, 3 } backtrack from 3 Visit next branch from 2 2 1 6 7 5 3 4 T = { 1, 2, 3, 4 } 2 1 6 7 5 3 4 T = { 1, 2, 3, 4, 7 }
  • 30. 30 T = { 1, 2, 3, 4, 7, 5 } 2 1 6 7 5 3 4 2 1 6 7 5 3 4 T = { 1, 2, 3, 4, 7, 5, 6 } 2 1 6 7 5 3 4 Final DFS tree.
  • 31. 31 Analysis: • The complexity of the algorithm is greatly affected by Traverse function we can write its running time in terms of the relation, T(n) = T(n-1) + O(n), • At each recursive call a vertex is decreased and for each vertex atmost n adjacent vertices can be there. So, O(n). • Solving this we get, T(n) = O(n2). This is the case when we use adjacency matrix. • If adjacency list is used, T(n) = O(n + e), where e is number of edges.
  • 32. 32 BFS • This is one of the simplest methods of graph searching. • Choose some vertex as a root or starting vertex. • Add it to the queue. Mark it as visited and dequeue it from the queue. • Add all the adjacent vertices of this node into queue. • Remove one node from front and mark it as visited. Repeat this process until all the nodes are visited. BFS (G, S) { Initialize Queue, q = { } mark S as visited; enqueue(q, S); while( q != Empty) { v = dequeue(q); for each w adjacent to v { if w is not marked as visited { enqueue(q, w) mark w as visited. } } } }
  • 33. 33 Analysis: • This algorithms puts all the vertices in the queue and they are accessed one by one. • for each accessed vertex from the queue their adjacent vertices are looked up for O(n) time (for worst case). • Total time complexity , T(n) = O(n2) , in case of adjacency matrix. • T(n) = O(n +e), in case of adjacency list.
  • 34. 34 2 1 6 7 5 3 4 Example: BFS Algorithm Tracing Solution: 2 1 6 7 5 3 4  Starting vertex : 1 Visited: { 1 } 2 1 6 7 5 3 4 Visited: { 1,2, 3, 7, 5, 6 } 1 2 3 7 5 6Queue : Queue :
  • 35. 35 2 1 6 7 5 3 4  Dequeue front of queue. Add its all unvisited adjacent nodes to the queue. Queue 3 7 5 6 4 Visited : { 1, 2, 3, 7, 5, 6, 4 } 2 1 6 7 5 3 4 Queue Visited : { 1, 2, 3, 7, 5, 6, 4 }
  • 37. 37 Spanning Tree A spanning tree of a connected undirected graph G is a sub graph T of without cycle G that connects all the vertices of G. i.e. A spanning tree for a connected graph G is a tree containing all the vertices of G • A minimum spanning tree in a connected weighted graph is a spanning tree that has the smallest possible sum of weights of its edges. • It represents the cheapest way of connecting all the nodes in G. • It is not necessarily unique. • Any time you want to visit all vertices in a graph at minimum cost (e.g., wire routing on printed circuit boards, sewer pipe layout, road planning…) Minimum Spanning Trees
  • 38. 38 • Two algorithms that are used to construct the minimum spanning tree from the given connected weighted graph of given graph are:  Kruskal's Algorithm  Prim's Algorithm MST Generating Algorithms Kruskal's Algorithm We have V as a set of n vertices and E as set of edges of graph G. The idea behind this algorithm is: • The nodes of the graph are considered as n distinct partial trees with one node each. • At each step of the algorithm, two partial trees are connected into single partial tree by an edge of the graph. • While connecting two nodes of partial trees, minimum weighted arc is selected. • After n-1 steps MST is obtained.
  • 39. 39
  • 40. 40
  • 41. 41 Algorithm: Kruskal_MSt( G ) { T = { V } // forest of n nodes S = Set of edges sorted in non-decreasing order of weight while( |T| < n-1 AND S != Empty) { select edge (u,v) from S in order S = S – (u,v) if (u,v) does not form cycle in T T = T U {(u,v)} } } Complexity Analysis To form the forest of n trees takes O(n) time, the creation of S takes O(E.log E) time and while loop executes O(n) time and the steps inside loop take almost linear time. So, total time – O(n) + O (E logE) +O(n log n)
  • 43. 43