SlideShare a Scribd company logo
1 of 52
CHAPTER 6
GRAPHS
Dr. R. Khanchana
Assistant Professor
Department of Computer Science
Sri Ramakrishna College of Arts and Science for
Women
CHAPTER 6 1
http://icodeguru.com/vc/10book/books/book1/chap06.htm
CHAPTER 6
2
Definition
 A graph G consists of two sets
– a finite, nonempty set of vertices
V(G)
– a finite, possible empty set of
edges E(G)
– G(V,E) represents a graph
Directed Graph Vs Undirected Graph
CHAPTER 6 3
 An undirected graph is
one in which the pair of
vertices in a edge is
unordered, (v0, v1) = (v1,v0)
 A directed graph is one in
which each edge is a
directed pair of vertices,
<v0, v1> != <v1,v0>
tail head
CHAPTER 6
4
Examples for Graph
1
2 3
4
1
2 3
4 5 6 7
G1
G2 G3
complete graph incomplete graph
CHAPTER 6 5
Complete Graph
 A complete graph is a graph that has the
maximum number of edges
– for undirected graph with n vertices, the
maximum number of edges is n(n-1)/2
– for directed graph with n vertices, the
maximum number of edges is n(n-1)
– Example:
– G1 is a complete undirected graph
– G2 is a complete directed graph
1
2 3
4 G1
G2
CHAPTER 6 6
Adjacent and Incident in
Undirected Graph
 If (v1, v2) is an edge in an undirected
graph
- v1 and v2 are adjacent
– The edge (v1, v2) is incident on
vertices v1 and v2
– Example In E(G), the vertices adjacent to
vertex 2 is 4,5 and 1
– The edges incident on vetex3 in E(G) are
(1,3), (3,6) and (3,7)
CHAPTER 6 7
Adjacent and Incident in
Directed Graph
 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
– Example In E(G), <3, 2> - the vertex 3
is adjacent to vertex 2 where vertex 2 is
adjacent from vertex 3
– The edge <3, 2> is incident to 3 and 2.
E(G)
CHAPTER 6 8
0 2
1
(a)
3
2
1
4
(b)
self edge multigraph:
multiple occurrences
of the same edge
Figure 6.3
Multigraph is not a graph
CHAPTER 6 9
 A subgraph of G is a graph G’ such that V(G’) is a subset of V(G)
and E(G’) is a subset of E(G)
Subgraph
1
2 3
4
G1
G3
CHAPTER 6 10
 A simple path is a path in which all vertices
except possibly the first and last are distinct
 The length of a path is the number of edges on it
Path in Graph
CHAPTER 6 11
 A cycle is a simple path in which the first and
the last vertices are the same
Cycle in Graph
Cycle
Path
CHAPTER 6
12
 A connected component of an undirected graph
is a maximal connected subgraph.
 In an undirected graph G, two vertices, v0 and v1, are
connected if there is a path in G from v0 to v1
Connected Components
CHAPTER 6 13
 A strongly connected component is a maximal
subgraph that is strongly connected.
 A directed graph is strongly connected if there
is a directed path from vi to vj and also
from vj to vi.
Strongly Connected Component
G3
CHAPTER 6 14
0
1 2
3
0
1 2
3 4 5 6
G1
G2
Tree (acyclic graph)
A tree is a graph that is connected and acyclic
Acyclic Graph
Connected
CHAPTER 6 15
Degree
 The degree of a vertex is the number of edges
incident to that vertex
 For directed graph,
– in-degree of a vertex v is the number of
edges that have v as the head
– out-degree of a vertex v is the number of
edges that have v as the tail
CHAPTER 6
16
Degree in Undirected graph
0
1 2
3 4 5 6
G1 G2
3
2
3 3
1 1 1 1
0
1 2
3
33
3
CHAPTER 6 17
Degree
CHAPTER 6
18
Degree in Directed graph
Directed graph
in-degree
out-degree
G3
indegree:1
outdegree: 1
indegree: 1
outdegree: 2
indegree: 1
outdegree: 0
Quiz
 https://quizizz.com/admin/quiz/5f5de89ead
5020001dab5a3a
CHAPTER 6 19
CHAPTER 6 20
Graph Representations
 Adjacency Matrix
 Adjacency Lists
 Adjacency Multilists
CHAPTER 6 21
Adjacency Matrix
 Let G=(V,E) be a graph with n vertices.
 The adjacency matrix of G is a two-dimensional
n * n array, say A(i,j)
 If the edge (vi, vj) is in E(G), A(i,j) =1
 If there is no such edge in E(G), A(i,j) =0
 The adjacency matrix for an undirected graph is
symmetric; the adjacency matrix for a digraph
need not be symmetric
CHAPTER 6 22
Examples for Adjacency Matrix
0
1
1
1
1
0
1
1
1
1
0
1
1
1
1
0












0
1
0
1
0
0
0
1
0










0
1
1
0
0
0
0
0
1
0
0
1
0
0
0
0
1
0
0
1
0
0
0
0
0
1
1
0
0
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0
1
0
0
0
0
0
0
1
0


























G1
G2
G4
1
2 3
4
1
2
3
2
1
3
4
5
6
7
8
symmetric
e is the No. of Edges
undirected:e<< n2/2
directed: e<n2
 The representation the n rows of the
adjacency matrix are represented as n linked
lists.
 There is one list for each vertex in G.
 Each node has at least two fields: VERTEX
and LINK
 The VERTEX fields contain the indices of
the vertices adjacent to vertex i.
CHAPTER 6 23
Adjacency Lists
Each row in adjacency matrix is represented as an adjacency list.
CHAPTER 6 24
Adjacency Lists- Undirected
Graph
The Adjacency lists for G1 is shown
CHAPTER 6 25
Adjacency Lists –Directed Graph
The Adjacency lists for G3 is shown
CHAPTER 6 26
Adjacency Lists- Connected
Components
The adjacency lists for G4 is shown
CHAPTER 6 27
Inverse Adjacency Lists
Inverse adjacency lists for G3
Each node would now have four fields and would represent one edge.
The node structure would be
tail head column link for head row link for tail
CHAPTER 6 28
Adjacency Multilists
An edge in an undirected graph is
represented by two nodes in adjacency list
representation.
Adjacency Multilists
–lists in which nodes may be shared among
several lists.
(an edge is shared by two different paths)
Adjacency Multilists
CHAPTER 6 29
Adjacency Multilists
CHAPTER 6 30
Adjacency List for G1
Adjacency Multilists
CHAPTER 6 31
CHAPTER 6 32
TRAVERSALS
 Traversal
Given G=(V,E) and vertex v, find all wV,
such that w connects v.
– Depth First Search (DFS)
preorder tree traversal
– Breadth First Search (BFS)
level order tree traversal
 Connected Components
 Spanning Trees
CHAPTER 6 33
Depth First Search (DFS)
If a depth first search is initiated from vertex v1,
then the vertices of G are visited in the
order: v1, v2, v4, v8, v5, v6, v3, v7.
CHAPTER 6 34
Breadth First Search (BFS)
A breadth first search beginning at vertex v1 of the graph.
First visit v1 and then v2 and v3. Next
vertices v4, v5, v6 and v7 will be visited and finally v8.
CHAPTER 6 35
Graph and its Adjacency List
CHAPTER 6 36
Connected Components
CHAPTER 6 37
Spanning Trees
 When graph G is connected, a depth first or
breadth first search starting at any vertex will
visit all vertices in G
 A spanning tree is any tree that consists solely
of edges in G and that includes all the vertices
 E(G): T (tree edges) + N (nontree edges)
where T: set of edges used during search
N: set of remaining edges
CHAPTER 6 38
Examples of Spanning Tree
G1 Possible spanning trees
CHAPTER 6 39
Spanning Trees
 Either DFS or BFS can be used to create a
spanning tree
– When DFS is used, the resulting spanning tree is
known as a depth first spanning tree
– When BFS is used, the resulting spanning tree is
known as a breadth first spanning tree
 While adding a nontree edge into any spanning
tree, this will create a cycle
CHAPTER 6 40
DFS VS BFS Spanning Tree
DFS Spanning BFS Spanning
CHAPTER 6 41
Minimum Cost Spanning Tree
 The cost of a spanning tree of a weighted
undirected graph is the sum of the costs of the
edges in the spanning tree
 A minimum cost spanning tree is a spanning
tree of least cost
 Three different algorithms can be used
– Kruskal
– Prim
– Sollin
Select n-1 edges from a weighted graph
of n vertices with minimum cost.
Minimum Cost Spanning Tree
CHAPTER 6 42
Kruskal’s Algorithm
CHAPTER 6 43
Kruskal’s Algorithm
CHAPTER 6 44
SHORTEST PATHS AND TRANSITIVE
CLOSURE
 Single Source All Destinations
CHAPTER 6 45
Analysis of Algorithm SHORTEST PATH
CHAPTER 6 46
Analysis of Algorithm SHORTEST PATH
CHAPTER 6 47
CHAPTER 6 48
All Pairs Shortest Paths
 Ak(i,j) = min {Ak-1(i,j), Ak-1(i,k) + Ak -1(K,j)}
K>=1 and
 Ao(i,j) = COST(i,j)
CHAPTER 6 49
All Pairs Shortest Paths
CHAPTER 6 50
Cost Matrix
CHAPTER 6 51
Transitive Closure
CHAPTER 6 52
Figure 6.25 Graph G and Its Adjacency
Matrix A, A+ and A*
A+(i,j) = 1 if path length >0
A*(i,j) = 1 if path length >=0

More Related Content

What's hot (20)

Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
Heap sort
Heap sort Heap sort
Heap sort
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Presentation on queue
Presentation on queuePresentation on queue
Presentation on queue
 
Binary tree traversal ppt - 02.03.2020
Binary tree traversal   ppt - 02.03.2020Binary tree traversal   ppt - 02.03.2020
Binary tree traversal ppt - 02.03.2020
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
 
Binary trees1
Binary trees1Binary trees1
Binary trees1
 
Quick sort
Quick sortQuick sort
Quick sort
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Expression trees
Expression treesExpression trees
Expression trees
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Stack project
Stack projectStack project
Stack project
 

Similar to Unit 3 graph chapter6

Grpahs in Data Structure
Grpahs in Data StructureGrpahs in Data Structure
Grpahs in Data StructureAvichalVishnoi
 
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsFallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsSnehilKeshari
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxasimshahzad8611
 
Cs6702 2marks rejinpaul
Cs6702 2marks rejinpaulCs6702 2marks rejinpaul
Cs6702 2marks rejinpaulstalinjothi
 
Cs6702 graph theory and applications 2 marks questions and answers
Cs6702 graph theory and applications 2 marks questions and answersCs6702 graph theory and applications 2 marks questions and answers
Cs6702 graph theory and applications 2 marks questions and answersappasami
 
Graph in Data Structure
Graph in Data StructureGraph in Data Structure
Graph in Data StructureProf Ansari
 
Elements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptxElements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptxmiki304759
 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabatinabati
 
1. Graph and Graph Terminologiesimp.pptx
1. Graph and Graph Terminologiesimp.pptx1. Graph and Graph Terminologiesimp.pptx
1. Graph and Graph Terminologiesimp.pptxswapnilbs2728
 

Similar to Unit 3 graph chapter6 (20)

Grpahs in Data Structure
Grpahs in Data StructureGrpahs in Data Structure
Grpahs in Data Structure
 
09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf
 
Dsa.PPT
Dsa.PPTDsa.PPT
Dsa.PPT
 
chapter6.PPT
chapter6.PPTchapter6.PPT
chapter6.PPT
 
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphsFallsem2015 16 cp4194-13-oct-2015_rm01_graphs
Fallsem2015 16 cp4194-13-oct-2015_rm01_graphs
 
Graph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptxGraph terminology and algorithm and tree.pptx
Graph terminology and algorithm and tree.pptx
 
Cs6702 2marks rejinpaul
Cs6702 2marks rejinpaulCs6702 2marks rejinpaul
Cs6702 2marks rejinpaul
 
Cs6702 GCC
Cs6702 GCCCs6702 GCC
Cs6702 GCC
 
Cs6702 graph theory and applications 2 marks questions and answers
Cs6702 graph theory and applications 2 marks questions and answersCs6702 graph theory and applications 2 marks questions and answers
Cs6702 graph theory and applications 2 marks questions and answers
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
 
Graph in Data Structure
Graph in Data StructureGraph in Data Structure
Graph in Data Structure
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
Elements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptxElements of Graph Theory for IS.pptx
Elements of Graph Theory for IS.pptx
 
Graphs
GraphsGraphs
Graphs
 
graph theory
graph theorygraph theory
graph theory
 
Chap 6 Graph.ppt
Chap 6 Graph.pptChap 6 Graph.ppt
Chap 6 Graph.ppt
 
Introduction to Graph Theory
Introduction to Graph TheoryIntroduction to Graph Theory
Introduction to Graph Theory
 
Graph theory concepts complex networks presents-rouhollah nabati
Graph theory concepts   complex networks presents-rouhollah nabatiGraph theory concepts   complex networks presents-rouhollah nabati
Graph theory concepts complex networks presents-rouhollah nabati
 
1. Graph and Graph Terminologiesimp.pptx
1. Graph and Graph Terminologiesimp.pptx1. Graph and Graph Terminologiesimp.pptx
1. Graph and Graph Terminologiesimp.pptx
 
FCS (graphs).pptx
FCS (graphs).pptxFCS (graphs).pptx
FCS (graphs).pptx
 

More from DrkhanchanaR

Unit 5 composite datatypes
Unit 5  composite datatypesUnit 5  composite datatypes
Unit 5 composite datatypesDrkhanchanaR
 
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLEUnit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLEDrkhanchanaR
 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expressionDrkhanchanaR
 
Unit I Database concepts - RDBMS & ORACLE
Unit I  Database concepts - RDBMS & ORACLEUnit I  Database concepts - RDBMS & ORACLE
Unit I Database concepts - RDBMS & ORACLEDrkhanchanaR
 

More from DrkhanchanaR (7)

Unit 5 composite datatypes
Unit 5  composite datatypesUnit 5  composite datatypes
Unit 5 composite datatypes
 
Unit 4 plsql
Unit 4  plsqlUnit 4  plsql
Unit 4 plsql
 
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLEUnit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
 
Unit 2 oracle9i
Unit 2  oracle9i Unit 2  oracle9i
Unit 2 oracle9i
 
Unit I - Evaluation of expression
Unit I - Evaluation of expressionUnit I - Evaluation of expression
Unit I - Evaluation of expression
 
Data Modeling
Data ModelingData Modeling
Data Modeling
 
Unit I Database concepts - RDBMS & ORACLE
Unit I  Database concepts - RDBMS & ORACLEUnit I  Database concepts - RDBMS & ORACLE
Unit I Database concepts - RDBMS & ORACLE
 

Recently uploaded

Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementmkooblal
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxabhijeetpadhi001
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 

Recently uploaded (20)

Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
Hierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of managementHierarchy of management that covers different levels of management
Hierarchy of management that covers different levels of management
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
MICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptxMICROBIOLOGY biochemical test detailed.pptx
MICROBIOLOGY biochemical test detailed.pptx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 

Unit 3 graph chapter6

  • 1. CHAPTER 6 GRAPHS Dr. R. Khanchana Assistant Professor Department of Computer Science Sri Ramakrishna College of Arts and Science for Women CHAPTER 6 1 http://icodeguru.com/vc/10book/books/book1/chap06.htm
  • 2. CHAPTER 6 2 Definition  A graph G consists of two sets – a finite, nonempty set of vertices V(G) – a finite, possible empty set of edges E(G) – G(V,E) represents a graph
  • 3. Directed Graph Vs Undirected Graph CHAPTER 6 3  An undirected graph is one in which the pair of vertices in a edge is unordered, (v0, v1) = (v1,v0)  A directed graph is one in which each edge is a directed pair of vertices, <v0, v1> != <v1,v0> tail head
  • 4. CHAPTER 6 4 Examples for Graph 1 2 3 4 1 2 3 4 5 6 7 G1 G2 G3 complete graph incomplete graph
  • 5. CHAPTER 6 5 Complete Graph  A complete graph is a graph that has the maximum number of edges – for undirected graph with n vertices, the maximum number of edges is n(n-1)/2 – for directed graph with n vertices, the maximum number of edges is n(n-1) – Example: – G1 is a complete undirected graph – G2 is a complete directed graph 1 2 3 4 G1 G2
  • 6. CHAPTER 6 6 Adjacent and Incident in Undirected Graph  If (v1, v2) is an edge in an undirected graph - v1 and v2 are adjacent – The edge (v1, v2) is incident on vertices v1 and v2 – Example In E(G), the vertices adjacent to vertex 2 is 4,5 and 1 – The edges incident on vetex3 in E(G) are (1,3), (3,6) and (3,7)
  • 7. CHAPTER 6 7 Adjacent and Incident in Directed Graph  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 – Example In E(G), <3, 2> - the vertex 3 is adjacent to vertex 2 where vertex 2 is adjacent from vertex 3 – The edge <3, 2> is incident to 3 and 2. E(G)
  • 8. CHAPTER 6 8 0 2 1 (a) 3 2 1 4 (b) self edge multigraph: multiple occurrences of the same edge Figure 6.3 Multigraph is not a graph
  • 9. CHAPTER 6 9  A subgraph of G is a graph G’ such that V(G’) is a subset of V(G) and E(G’) is a subset of E(G) Subgraph 1 2 3 4 G1 G3
  • 10. CHAPTER 6 10  A simple path is a path in which all vertices except possibly the first and last are distinct  The length of a path is the number of edges on it Path in Graph
  • 11. CHAPTER 6 11  A cycle is a simple path in which the first and the last vertices are the same Cycle in Graph Cycle Path
  • 12. CHAPTER 6 12  A connected component of an undirected graph is a maximal connected subgraph.  In an undirected graph G, two vertices, v0 and v1, are connected if there is a path in G from v0 to v1 Connected Components
  • 13. CHAPTER 6 13  A strongly connected component is a maximal subgraph that is strongly connected.  A directed graph is strongly connected if there is a directed path from vi to vj and also from vj to vi. Strongly Connected Component G3
  • 14. CHAPTER 6 14 0 1 2 3 0 1 2 3 4 5 6 G1 G2 Tree (acyclic graph) A tree is a graph that is connected and acyclic Acyclic Graph Connected
  • 15. CHAPTER 6 15 Degree  The degree of a vertex is the number of edges incident to that vertex  For directed graph, – in-degree of a vertex v is the number of edges that have v as the head – out-degree of a vertex v is the number of edges that have v as the tail
  • 16. CHAPTER 6 16 Degree in Undirected graph 0 1 2 3 4 5 6 G1 G2 3 2 3 3 1 1 1 1 0 1 2 3 33 3
  • 18. CHAPTER 6 18 Degree in Directed graph Directed graph in-degree out-degree G3 indegree:1 outdegree: 1 indegree: 1 outdegree: 2 indegree: 1 outdegree: 0
  • 20. CHAPTER 6 20 Graph Representations  Adjacency Matrix  Adjacency Lists  Adjacency Multilists
  • 21. CHAPTER 6 21 Adjacency Matrix  Let G=(V,E) be a graph with n vertices.  The adjacency matrix of G is a two-dimensional n * n array, say A(i,j)  If the edge (vi, vj) is in E(G), A(i,j) =1  If there is no such edge in E(G), A(i,j) =0  The adjacency matrix for an undirected graph is symmetric; the adjacency matrix for a digraph need not be symmetric
  • 22. CHAPTER 6 22 Examples for Adjacency Matrix 0 1 1 1 1 0 1 1 1 1 0 1 1 1 1 0             0 1 0 1 0 0 0 1 0           0 1 1 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 1 0                           G1 G2 G4 1 2 3 4 1 2 3 2 1 3 4 5 6 7 8 symmetric e is the No. of Edges undirected:e<< n2/2 directed: e<n2
  • 23.  The representation the n rows of the adjacency matrix are represented as n linked lists.  There is one list for each vertex in G.  Each node has at least two fields: VERTEX and LINK  The VERTEX fields contain the indices of the vertices adjacent to vertex i. CHAPTER 6 23 Adjacency Lists Each row in adjacency matrix is represented as an adjacency list.
  • 24. CHAPTER 6 24 Adjacency Lists- Undirected Graph The Adjacency lists for G1 is shown
  • 25. CHAPTER 6 25 Adjacency Lists –Directed Graph The Adjacency lists for G3 is shown
  • 26. CHAPTER 6 26 Adjacency Lists- Connected Components The adjacency lists for G4 is shown
  • 27. CHAPTER 6 27 Inverse Adjacency Lists Inverse adjacency lists for G3 Each node would now have four fields and would represent one edge. The node structure would be tail head column link for head row link for tail
  • 28. CHAPTER 6 28 Adjacency Multilists An edge in an undirected graph is represented by two nodes in adjacency list representation. Adjacency Multilists –lists in which nodes may be shared among several lists. (an edge is shared by two different paths)
  • 30. Adjacency Multilists CHAPTER 6 30 Adjacency List for G1
  • 32. CHAPTER 6 32 TRAVERSALS  Traversal Given G=(V,E) and vertex v, find all wV, such that w connects v. – Depth First Search (DFS) preorder tree traversal – Breadth First Search (BFS) level order tree traversal  Connected Components  Spanning Trees
  • 33. CHAPTER 6 33 Depth First Search (DFS) If a depth first search is initiated from vertex v1, then the vertices of G are visited in the order: v1, v2, v4, v8, v5, v6, v3, v7.
  • 34. CHAPTER 6 34 Breadth First Search (BFS) A breadth first search beginning at vertex v1 of the graph. First visit v1 and then v2 and v3. Next vertices v4, v5, v6 and v7 will be visited and finally v8.
  • 35. CHAPTER 6 35 Graph and its Adjacency List
  • 37. CHAPTER 6 37 Spanning Trees  When graph G is connected, a depth first or breadth first search starting at any vertex will visit all vertices in G  A spanning tree is any tree that consists solely of edges in G and that includes all the vertices  E(G): T (tree edges) + N (nontree edges) where T: set of edges used during search N: set of remaining edges
  • 38. CHAPTER 6 38 Examples of Spanning Tree G1 Possible spanning trees
  • 39. CHAPTER 6 39 Spanning Trees  Either DFS or BFS can be used to create a spanning tree – When DFS is used, the resulting spanning tree is known as a depth first spanning tree – When BFS is used, the resulting spanning tree is known as a breadth first spanning tree  While adding a nontree edge into any spanning tree, this will create a cycle
  • 40. CHAPTER 6 40 DFS VS BFS Spanning Tree DFS Spanning BFS Spanning
  • 41. CHAPTER 6 41 Minimum Cost Spanning Tree  The cost of a spanning tree of a weighted undirected graph is the sum of the costs of the edges in the spanning tree  A minimum cost spanning tree is a spanning tree of least cost  Three different algorithms can be used – Kruskal – Prim – Sollin Select n-1 edges from a weighted graph of n vertices with minimum cost.
  • 42. Minimum Cost Spanning Tree CHAPTER 6 42
  • 45. SHORTEST PATHS AND TRANSITIVE CLOSURE  Single Source All Destinations CHAPTER 6 45
  • 46. Analysis of Algorithm SHORTEST PATH CHAPTER 6 46
  • 47. Analysis of Algorithm SHORTEST PATH CHAPTER 6 47
  • 49. All Pairs Shortest Paths  Ak(i,j) = min {Ak-1(i,j), Ak-1(i,k) + Ak -1(K,j)} K>=1 and  Ao(i,j) = COST(i,j) CHAPTER 6 49
  • 50. All Pairs Shortest Paths CHAPTER 6 50
  • 52. Transitive Closure CHAPTER 6 52 Figure 6.25 Graph G and Its Adjacency Matrix A, A+ and A* A+(i,j) = 1 if path length >0 A*(i,j) = 1 if path length >=0