SlideShare a Scribd company logo
1 of 45
Download to read offline
Graphs
Graph Representation
• Adjacency Matrix
• Adjacency Lists
 Linked Adjacency Lists
 Packed (Array) Adjacency Lists
Types of Graphs
• Undirected
• Directed
• Weighted
Graphs
• G = (V,E), an ordered pair
• V is the vertex set.
• Vertices are also called nodes and points.
• E is the edge set.
• Each edge connects two different vertices.
• Edges are also called arcs and lines.
• Directed edge has an orientation (u,v).
u v
Graphs
• Undirected edge has no orientation (u,v).
u v
• Undirected graph => no oriented edge.
• Directed graph => every edge has an
orientation.
Undirected Graph
2
3
8
10
1
4 5 9
11
6
7
Directed Graph (Digraph)
2
3
8
1
0
1
4
5 9 11
6
7
Applications—Communication Network
• Vertex = city, edge = communication link.
2
3
8
1
0
1
4
5 9 11
6
7
Driving Distance/Time Map
• Vertex = city, edge weight = driving
distance/time.
2
3
8
1
0
1
4
5 9 11
6
7
4
8
6
6
7
5
2
4
4 5
3
Street Map
• Some streets are one way.
2
3
8
1
0
1
4
5 9 11
6
7
Connected Graph
• Undirected graph.
• There is a path between every pair of
vertices.
Example Of Not Connected
2
3
8
10
1
4
5
9
11
6
7
Connected Graph Example
2
3
8
10
1
4
5
9
11
6
7
Connected Components
2
3
8
10
1
4
5
9
11
6
7
Even More Terminology
• connected component: maximal connected subgraph. e.g., the graph below has
3 connected components.
Terminology:
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
17
Graph – An Example
1
4
2
5
3
A graph G (undirected)
The graph G= (V,E) has 5 vertices and 6 edges:
V = {1,2,3,4,5}
E = { (1,2),(1,3),(1,4),(2,5),(3,4),(4,5)}
• Adjacent:
1 and 2 are adjacent -- 1 is adjacent to 2 and 2 is adjacent to 1
18
Directed Graph – An Example
1
4
2
5
3
The graph G= (V,E) has 5 vertices and 6 edges:
V = {1,2,3,4,5}
E = { (1,2),(1,4),(2,5),(4,5),(3,1),(4,3) }
• Adjacent:
1 is adjacent to 2, but 2 is NOT adjacent to 1
Set notation of a Graph
• 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)}
Applications
• Traffic Flow
• Communication System
• Supply chain
• Electrical Network
• Airlines
• Social Network (Graph Search)
Weighted Graph
2
3
8
10
1
4
5
9
11
6
7
4
8
6
6
7
5
2
4
4 5
3
8
2
Subgraphs
• A subgraph consists of a subset of a graph’s
vertices and a subset of its edges.
• Let G=(V,E) and )
,
( E
V
G 



E
V
G
G 




 E
and
V
iff
0 0
1 2 3
1 2 0
1 2
3
(i) (ii) (iii) (iv)
(a) Some of the subgraph of G1
0 0
1
0
1
2
0
1
2
(i) (ii) (iii) (iv)
(b) Some of the subgraph of G3
0
1 2
3
G1
0
1
2
G3
Subgraphs Examples
Complete Graph
• Let n = #vertices, and m = #edges
• A complete graph: one in which all pairs of vertices are
adjacent
• How many total edges in a complete graph?
– Each of the n vertices is incident to n-1 edges, however, we would
have counted each edge twice! Therefore, intuitively, m = n(n -1)/2.
• Therefore, if a graph is not complete, m < n(n -1)/2
• Every complete graph is connected but
the converse need not be true.
1
4
2
5
3
Connected but not complete
Complete Undirected Graph
Has all possible edges.
n = 1 n = 2 n = 3 n = 4
27
Terminology:
Path
• path: sequence of
vertices v1,v2,. . .vk such
that consecutive vertices vi
and vi+1 are adjacent.
3
3 3
3
2
a b
c
d e
a b
c
d e
a b e d c b e d c
More Terminology
• simple path: 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
a c d a
a b
c
d e
29
Directed Graph – An Example
1
4
2
5
3
The graph G= (V,E) has 5 vertices and 6 edges:
V = {1,2,3,4,5}
E = { (1,2),(1,4),(2,5),(4,5),(3,1),(4,3) }
•Path:
1,2,5 ( a simple directed path) but 1,4,3,1,2 is not a simple path.
• Cycle:
1,4,3,1 (a directed cycle),
Tree
•A connected undirected graph that has no cycle
1
E
then
if 

 n
n
V
31
Spanning trees
• spanning tree of a graph is a connected subgraph
in which there are no cycles
A connected,
undirected graph
Four of the spanning trees of the graph
• A subgraph of G contains all the vertices of
G and is a tree is called spanning tree.
Bipartite graph: a graph whose vertices can be partitioned into two
disjoint sets V and U, not necessarily of the same size, so that every
edge connects a vertex in V to a vertex in U.
A graph is bipartite if and only if it does not have a cycle of an odd
length.
4 5
11
10
9
8
7
1 2 3
V
U
6
12
Bipartite trees
Complete Bipartite Graphs
K3,3
Degree of a vertex in an
undirected graph
• The degree (or valency) of a vertex of a
graph is the number of edges incident to the
vertex
2
3
1
4
5
d(1)=2
d(2)=2
d(3)=1
d(5)=3
d(4)=2
Handshaking lemma
• Let G(V,E) be an undirected graph. Let
and then
(a)
(b)
Proof:
(a) Every edge is incident on exactly two
vertices. Hence the sum of the vertices equals
two times the number of edges.
n
V 
e
E 
e
d
n
i
i 2
1



2
/
)
1
(
0 

 n
n
e
Degree of a vertex in a directed
graph
1 2
3 4
1
1
1 
 out
in
d
d
2
and
1 2
2 
 out
in
d
d
1
and
2 4
4 
 out
in
d
d
2
3
3 
 out
in
d
d
Handshaking lemma
• Let G(V,E) be an directed graph. Let
and then
(a)
(b)
Proof:
(a) Every edge is incident on exactly two
vertices. Hence the sum of the vertices equals
two times the number of edges.
n
V 
e
E 
e
d
d
n
i
out
i
n
i
in
i 2
1
1

 
 

)
1
(
0 

 n
n
e
Adjacency Matrix
• 0/1 n x n matrix, where n = # of vertices
• A(i,j) = 1 iff (i,j) is an edge
2
3
1
4
5
1 2 3 4 5
1
2
3
4
5
0 1 0 1 0
1 0 0 0 1
0 0 0 0 1
1 0 0 0 1
0 1 1 1 0
Adjacency Matrix Properties
2
3
1
4
5
1 2 3 4 5
1
2
3
4
5
0 1 0 1 0
1 0 0 0 1
0 0 0 0 1
1 0 0 0 1
0 1 1 1 0
•Diagonal entries are zero.
•Adjacency matrix of an undirected graph is
symmetric.
A(i,j) = A(j,i) for all i and j.
Adjacency Matrix (Digraph)
2
3
1
4
5
1 2 3 4 5
1
2
3
4
5
0 0 0 1 0
1 0 0 0 1
0 0 0 0 0
0 0 0 0 1
0 1 1 0 0
•Diagonal entries are zero.
•Adjacency matrix of a digraph need not be
symmetric.
Adjacency Matrix
• n2 bits of space
• For an undirected graph, may store only
lower or upper triangle (exclude diagonal).
 (n-1)n/2 bits
• O(n) time to find vertex degree and/or
vertices adjacent to a given vertex.
Packed adjacency Lists
2
3
1
4
Linked Adjacency Lists
• Each adjacency list is a chain.
2
3
1
4
5
aList[1]
aList[5]
[2]
[3]
[4]
2 4
1 5
5
5 1
2 4 3
Array Length = n
# of chain nodes = 2e (undirected graph)
# of chain nodes = e (digraph)
Dijkstra’s shortest path problem
CS-102 Data Structure lectures on Graphs

More Related Content

Similar to CS-102 Data Structure lectures on Graphs

Matrix representation of graph
Matrix representation of graphMatrix representation of graph
Matrix representation of graphRounak Biswas
 
Matrix representation of graph
Matrix representation of graphMatrix representation of graph
Matrix representation of graphRounak Biswas
 
Ppt of graph theory
Ppt of graph theoryPpt of graph theory
Ppt of graph theoryArvindBorge
 
Ppt of graph theory
Ppt of graph theoryPpt of graph theory
Ppt of graph theoryArvindBorge
 
graph.pptx
graph.pptxgraph.pptx
graph.pptxhijigaf
 
graph.pptx
graph.pptxgraph.pptx
graph.pptxhijigaf
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjtepournima055
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjtepournima055
 
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
 
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
 

Similar to CS-102 Data Structure lectures on Graphs (20)

Graphs
GraphsGraphs
Graphs
 
Graphs
GraphsGraphs
Graphs
 
6. Graphs
6. Graphs6. Graphs
6. Graphs
 
6. Graphs
6. Graphs6. Graphs
6. Graphs
 
Matrix representation of graph
Matrix representation of graphMatrix representation of graph
Matrix representation of graph
 
Matrix representation of graph
Matrix representation of graphMatrix representation of graph
Matrix representation of graph
 
Ppt of graph theory
Ppt of graph theoryPpt of graph theory
Ppt of graph theory
 
Ppt of graph theory
Ppt of graph theoryPpt of graph theory
Ppt of graph theory
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
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
 
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
 

More from ssuser034ce1

CS-102 Data Structures huffman coding.pdf
CS-102 Data Structures huffman coding.pdfCS-102 Data Structures huffman coding.pdf
CS-102 Data Structures huffman coding.pdfssuser034ce1
 
CS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdfCS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdfssuser034ce1
 
CS-102 DS-class04a Lectures DS Class.pdf
CS-102 DS-class04a Lectures DS Class.pdfCS-102 DS-class04a Lectures DS Class.pdf
CS-102 DS-class04a Lectures DS Class.pdfssuser034ce1
 
CS-102 DS-class03 Class DS Lectures .pdf
CS-102 DS-class03 Class DS Lectures .pdfCS-102 DS-class03 Class DS Lectures .pdf
CS-102 DS-class03 Class DS Lectures .pdfssuser034ce1
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfCS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfssuser034ce1
 
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfssuser034ce1
 
CS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdfCS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdfssuser034ce1
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfssuser034ce1
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfssuser034ce1
 
SURA presentation_.pptx
SURA presentation_.pptxSURA presentation_.pptx
SURA presentation_.pptxssuser034ce1
 

More from ssuser034ce1 (12)

CS-102 Data Structures huffman coding.pdf
CS-102 Data Structures huffman coding.pdfCS-102 Data Structures huffman coding.pdf
CS-102 Data Structures huffman coding.pdf
 
CS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdfCS-102 Data Structures HashFunction CS102.pdf
CS-102 Data Structures HashFunction CS102.pdf
 
CS-102 DS-class04a Lectures DS Class.pdf
CS-102 DS-class04a Lectures DS Class.pdfCS-102 DS-class04a Lectures DS Class.pdf
CS-102 DS-class04a Lectures DS Class.pdf
 
CS-102 DS-class03 Class DS Lectures .pdf
CS-102 DS-class03 Class DS Lectures .pdfCS-102 DS-class03 Class DS Lectures .pdf
CS-102 DS-class03 Class DS Lectures .pdf
 
CS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdfCS-102 DS-class_01_02 Lectures Data .pdf
CS-102 DS-class_01_02 Lectures Data .pdf
 
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdfCS-102 BT_24_3_14 Binary Tree Lectures.pdf
CS-102 BT_24_3_14 Binary Tree Lectures.pdf
 
CS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdfCS-102 Course_ Binary Tree Lectures .pdf
CS-102 Course_ Binary Tree Lectures .pdf
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdf
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
 
CS-102 AVLSv2.pdf
CS-102 AVLSv2.pdfCS-102 AVLSv2.pdf
CS-102 AVLSv2.pdf
 
CS-102 AVLS.pdf
CS-102 AVLS.pdfCS-102 AVLS.pdf
CS-102 AVLS.pdf
 
SURA presentation_.pptx
SURA presentation_.pptxSURA presentation_.pptx
SURA presentation_.pptx
 

Recently uploaded

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 

Recently uploaded (20)

Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 

CS-102 Data Structure lectures on Graphs

  • 2. Graph Representation • Adjacency Matrix • Adjacency Lists  Linked Adjacency Lists  Packed (Array) Adjacency Lists
  • 3. Types of Graphs • Undirected • Directed • Weighted
  • 4. Graphs • G = (V,E), an ordered pair • V is the vertex set. • Vertices are also called nodes and points. • E is the edge set. • Each edge connects two different vertices. • Edges are also called arcs and lines. • Directed edge has an orientation (u,v). u v
  • 5. Graphs • Undirected edge has no orientation (u,v). u v • Undirected graph => no oriented edge. • Directed graph => every edge has an orientation.
  • 8. Applications—Communication Network • Vertex = city, edge = communication link. 2 3 8 1 0 1 4 5 9 11 6 7
  • 9. Driving Distance/Time Map • Vertex = city, edge weight = driving distance/time. 2 3 8 1 0 1 4 5 9 11 6 7 4 8 6 6 7 5 2 4 4 5 3
  • 10. Street Map • Some streets are one way. 2 3 8 1 0 1 4 5 9 11 6 7
  • 11. Connected Graph • Undirected graph. • There is a path between every pair of vertices.
  • 12. Example Of Not Connected 2 3 8 10 1 4 5 9 11 6 7
  • 15. Even More Terminology • connected component: maximal connected subgraph. e.g., the graph below has 3 connected components.
  • 16. Terminology: 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
  • 17. 17 Graph – An Example 1 4 2 5 3 A graph G (undirected) The graph G= (V,E) has 5 vertices and 6 edges: V = {1,2,3,4,5} E = { (1,2),(1,3),(1,4),(2,5),(3,4),(4,5)} • Adjacent: 1 and 2 are adjacent -- 1 is adjacent to 2 and 2 is adjacent to 1
  • 18. 18 Directed Graph – An Example 1 4 2 5 3 The graph G= (V,E) has 5 vertices and 6 edges: V = {1,2,3,4,5} E = { (1,2),(1,4),(2,5),(4,5),(3,1),(4,3) } • Adjacent: 1 is adjacent to 2, but 2 is NOT adjacent to 1
  • 19. Set notation of a Graph • 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)}
  • 20. Applications • Traffic Flow • Communication System • Supply chain • Electrical Network • Airlines • Social Network (Graph Search)
  • 22. Subgraphs • A subgraph consists of a subset of a graph’s vertices and a subset of its edges. • Let G=(V,E) and ) , ( E V G     E V G G       E and V iff
  • 23. 0 0 1 2 3 1 2 0 1 2 3 (i) (ii) (iii) (iv) (a) Some of the subgraph of G1 0 0 1 0 1 2 0 1 2 (i) (ii) (iii) (iv) (b) Some of the subgraph of G3 0 1 2 3 G1 0 1 2 G3 Subgraphs Examples
  • 24. Complete Graph • Let n = #vertices, and m = #edges • A complete graph: one in which all pairs of vertices are adjacent • How many total edges in a complete graph? – Each of the n vertices is incident to n-1 edges, however, we would have counted each edge twice! Therefore, intuitively, m = n(n -1)/2. • Therefore, if a graph is not complete, m < n(n -1)/2
  • 25. • Every complete graph is connected but the converse need not be true. 1 4 2 5 3 Connected but not complete
  • 26. Complete Undirected Graph Has all possible edges. n = 1 n = 2 n = 3 n = 4
  • 27. 27 Terminology: Path • path: sequence of vertices v1,v2,. . .vk such that consecutive vertices vi and vi+1 are adjacent. 3 3 3 3 2 a b c d e a b c d e a b e d c b e d c
  • 28. More Terminology • simple path: 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 a c d a a b c d e
  • 29. 29 Directed Graph – An Example 1 4 2 5 3 The graph G= (V,E) has 5 vertices and 6 edges: V = {1,2,3,4,5} E = { (1,2),(1,4),(2,5),(4,5),(3,1),(4,3) } •Path: 1,2,5 ( a simple directed path) but 1,4,3,1,2 is not a simple path. • Cycle: 1,4,3,1 (a directed cycle),
  • 30. Tree •A connected undirected graph that has no cycle 1 E then if    n n V
  • 31. 31 Spanning trees • spanning tree of a graph is a connected subgraph in which there are no cycles A connected, undirected graph Four of the spanning trees of the graph • A subgraph of G contains all the vertices of G and is a tree is called spanning tree.
  • 32. Bipartite graph: a graph whose vertices can be partitioned into two disjoint sets V and U, not necessarily of the same size, so that every edge connects a vertex in V to a vertex in U. A graph is bipartite if and only if it does not have a cycle of an odd length. 4 5 11 10 9 8 7 1 2 3 V U 6 12 Bipartite trees
  • 34. Degree of a vertex in an undirected graph • The degree (or valency) of a vertex of a graph is the number of edges incident to the vertex 2 3 1 4 5 d(1)=2 d(2)=2 d(3)=1 d(5)=3 d(4)=2
  • 35. Handshaking lemma • Let G(V,E) be an undirected graph. Let and then (a) (b) Proof: (a) Every edge is incident on exactly two vertices. Hence the sum of the vertices equals two times the number of edges. n V  e E  e d n i i 2 1    2 / ) 1 ( 0    n n e
  • 36. Degree of a vertex in a directed graph 1 2 3 4 1 1 1   out in d d 2 and 1 2 2   out in d d 1 and 2 4 4   out in d d 2 3 3   out in d d
  • 37. Handshaking lemma • Let G(V,E) be an directed graph. Let and then (a) (b) Proof: (a) Every edge is incident on exactly two vertices. Hence the sum of the vertices equals two times the number of edges. n V  e E  e d d n i out i n i in i 2 1 1       ) 1 ( 0    n n e
  • 38. Adjacency Matrix • 0/1 n x n matrix, where n = # of vertices • A(i,j) = 1 iff (i,j) is an edge 2 3 1 4 5 1 2 3 4 5 1 2 3 4 5 0 1 0 1 0 1 0 0 0 1 0 0 0 0 1 1 0 0 0 1 0 1 1 1 0
  • 39. Adjacency Matrix Properties 2 3 1 4 5 1 2 3 4 5 1 2 3 4 5 0 1 0 1 0 1 0 0 0 1 0 0 0 0 1 1 0 0 0 1 0 1 1 1 0 •Diagonal entries are zero. •Adjacency matrix of an undirected graph is symmetric. A(i,j) = A(j,i) for all i and j.
  • 40. Adjacency Matrix (Digraph) 2 3 1 4 5 1 2 3 4 5 1 2 3 4 5 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 1 0 0 •Diagonal entries are zero. •Adjacency matrix of a digraph need not be symmetric.
  • 41. Adjacency Matrix • n2 bits of space • For an undirected graph, may store only lower or upper triangle (exclude diagonal).  (n-1)n/2 bits • O(n) time to find vertex degree and/or vertices adjacent to a given vertex.
  • 43. Linked Adjacency Lists • Each adjacency list is a chain. 2 3 1 4 5 aList[1] aList[5] [2] [3] [4] 2 4 1 5 5 5 1 2 4 3 Array Length = n # of chain nodes = 2e (undirected graph) # of chain nodes = e (digraph)