SlideShare a Scribd company logo
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
Ad

More Related Content

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

Graph Theory
Graph TheoryGraph Theory
Graph Theory
Rashmi Bhat
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
Rashmi Bhat
 
Graphs
GraphsGraphs
Graphs
Dwight Sabio
 
Graphs
GraphsGraphs
Graphs
Dwight Sabio
 
Graphs
GraphsGraphs
Graphs
amudha arul
 
Graphs
GraphsGraphs
Graphs
amudha arul
 
6. Graphs
6. Graphs6. Graphs
6. Graphs
Mandeep Singh
 
6. Graphs
6. Graphs6. Graphs
6. Graphs
Mandeep Singh
 
An introduction to the graph theory in discrete mathematics
An introduction to the graph theory in discrete mathematicsAn introduction to the graph theory in discrete mathematics
An introduction to the graph theory in discrete mathematics
hassanbokhari14
 
An introduction to the graph theory in discrete mathematics
An introduction to the graph theory in discrete mathematicsAn introduction to the graph theory in discrete mathematics
An introduction to the graph theory in discrete mathematics
hassanbokhari14
 
Matrix representation of graph
Matrix representation of graphMatrix representation of graph
Matrix representation of graph
Rounak Biswas
 
Matrix representation of graph
Matrix representation of graphMatrix representation of graph
Matrix representation of graph
Rounak Biswas
 
Lecture 4- Design Analysis Of ALgorithms
Lecture 4- Design Analysis Of ALgorithmsLecture 4- Design Analysis Of ALgorithms
Lecture 4- Design Analysis Of ALgorithms
mtahanasir65
 
Lecture 4- Design Analysis Of ALgorithms
Lecture 4- Design Analysis Of ALgorithmsLecture 4- Design Analysis Of ALgorithms
Lecture 4- Design Analysis Of ALgorithms
mtahanasir65
 
Ppt of graph theory
Ppt of graph theoryPpt of graph theory
Ppt of graph theory
ArvindBorge
 
Ppt of graph theory
Ppt of graph theoryPpt of graph theory
Ppt of graph theory
ArvindBorge
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
hijigaf
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
hijigaf
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
pournima055
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
pournima055
 
An introduction to the graph theory in discrete mathematics
An introduction to the graph theory in discrete mathematicsAn introduction to the graph theory in discrete mathematics
An introduction to the graph theory in discrete mathematics
hassanbokhari14
 
An introduction to the graph theory in discrete mathematics
An introduction to the graph theory in discrete mathematicsAn introduction to the graph theory in discrete mathematics
An introduction to the graph theory in discrete mathematics
hassanbokhari14
 
Matrix representation of graph
Matrix representation of graphMatrix representation of graph
Matrix representation of graph
Rounak Biswas
 
Matrix representation of graph
Matrix representation of graphMatrix representation of graph
Matrix representation of graph
Rounak Biswas
 
Lecture 4- Design Analysis Of ALgorithms
Lecture 4- Design Analysis Of ALgorithmsLecture 4- Design Analysis Of ALgorithms
Lecture 4- Design Analysis Of ALgorithms
mtahanasir65
 
Lecture 4- Design Analysis Of ALgorithms
Lecture 4- Design Analysis Of ALgorithmsLecture 4- Design Analysis Of ALgorithms
Lecture 4- Design Analysis Of ALgorithms
mtahanasir65
 
Ppt of graph theory
Ppt of graph theoryPpt of graph theory
Ppt of graph theory
ArvindBorge
 
Ppt of graph theory
Ppt of graph theoryPpt of graph theory
Ppt of graph theory
ArvindBorge
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
hijigaf
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
hijigaf
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
pournima055
 
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjteUnit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
Unit II_Graph.pptxkgjrekjgiojtoiejhgnltegjte
pournima055
 

More from ssuser034ce1 (20)

CSN221_Lec_27 Computer Architecture and Microprocessor
CSN221_Lec_27 Computer Architecture and MicroprocessorCSN221_Lec_27 Computer Architecture and Microprocessor
CSN221_Lec_27 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_26 Computer Architecture and Microprocessor
CSN221_Lec_26 Computer Architecture and MicroprocessorCSN221_Lec_26 Computer Architecture and Microprocessor
CSN221_Lec_26 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_25 Computer Architecture and Microprocessor
CSN221_Lec_25 Computer Architecture and MicroprocessorCSN221_Lec_25 Computer Architecture and Microprocessor
CSN221_Lec_25 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_36 Computer Architecture and Microprocessor
CSN221_Lec_36 Computer Architecture and MicroprocessorCSN221_Lec_36 Computer Architecture and Microprocessor
CSN221_Lec_36 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_35 Computer Architecture and Microprocessor
CSN221_Lec_35 Computer Architecture and MicroprocessorCSN221_Lec_35 Computer Architecture and Microprocessor
CSN221_Lec_35 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_34 Computer Architecture and Microprocessor
CSN221_Lec_34 Computer Architecture and MicroprocessorCSN221_Lec_34 Computer Architecture and Microprocessor
CSN221_Lec_34 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_22.pdf Computer Architecture and Microprocessor
CSN221_Lec_22.pdf Computer Architecture and MicroprocessorCSN221_Lec_22.pdf Computer Architecture and Microprocessor
CSN221_Lec_22.pdf Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_17.pdf Multi Cycle Datapath Design
CSN221_Lec_17.pdf Multi Cycle Datapath DesignCSN221_Lec_17.pdf Multi Cycle Datapath Design
CSN221_Lec_17.pdf Multi Cycle Datapath Design
ssuser034ce1
 
CSN221_Lec_16.pdf MIPS ISA and Datapath design
CSN221_Lec_16.pdf MIPS ISA and Datapath designCSN221_Lec_16.pdf MIPS ISA and Datapath design
CSN221_Lec_16.pdf MIPS ISA and Datapath design
ssuser034ce1
 
CSN221_Lec_15.pdf MIPS ISA and Datapath design
CSN221_Lec_15.pdf MIPS ISA and Datapath designCSN221_Lec_15.pdf MIPS ISA and Datapath design
CSN221_Lec_15.pdf MIPS ISA and Datapath design
ssuser034ce1
 
Computer Architecture CSN221_Lec_37_SpecialTopics.pdf
Computer Architecture CSN221_Lec_37_SpecialTopics.pdfComputer Architecture CSN221_Lec_37_SpecialTopics.pdf
Computer Architecture CSN221_Lec_37_SpecialTopics.pdf
ssuser034ce1
 
CSN221_Lec_5.pdf Computer Organization, CPU Structure and Functions
CSN221_Lec_5.pdf Computer Organization, CPU Structure and FunctionsCSN221_Lec_5.pdf Computer Organization, CPU Structure and Functions
CSN221_Lec_5.pdf Computer Organization, CPU Structure and Functions
ssuser034ce1
 
CSN221_Lec_4.pdf Computer Organization & Architecture
CSN221_Lec_4.pdf Computer Organization & ArchitectureCSN221_Lec_4.pdf Computer Organization & Architecture
CSN221_Lec_4.pdf Computer Organization & Architecture
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.pdf
ssuser034ce1
 
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
ssuser034ce1
 
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
ssuser034ce1
 
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
ssuser034ce1
 
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
ssuser034ce1
 
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
ssuser034ce1
 
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
ssuser034ce1
 
CSN221_Lec_27 Computer Architecture and Microprocessor
CSN221_Lec_27 Computer Architecture and MicroprocessorCSN221_Lec_27 Computer Architecture and Microprocessor
CSN221_Lec_27 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_26 Computer Architecture and Microprocessor
CSN221_Lec_26 Computer Architecture and MicroprocessorCSN221_Lec_26 Computer Architecture and Microprocessor
CSN221_Lec_26 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_25 Computer Architecture and Microprocessor
CSN221_Lec_25 Computer Architecture and MicroprocessorCSN221_Lec_25 Computer Architecture and Microprocessor
CSN221_Lec_25 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_36 Computer Architecture and Microprocessor
CSN221_Lec_36 Computer Architecture and MicroprocessorCSN221_Lec_36 Computer Architecture and Microprocessor
CSN221_Lec_36 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_35 Computer Architecture and Microprocessor
CSN221_Lec_35 Computer Architecture and MicroprocessorCSN221_Lec_35 Computer Architecture and Microprocessor
CSN221_Lec_35 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_34 Computer Architecture and Microprocessor
CSN221_Lec_34 Computer Architecture and MicroprocessorCSN221_Lec_34 Computer Architecture and Microprocessor
CSN221_Lec_34 Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_22.pdf Computer Architecture and Microprocessor
CSN221_Lec_22.pdf Computer Architecture and MicroprocessorCSN221_Lec_22.pdf Computer Architecture and Microprocessor
CSN221_Lec_22.pdf Computer Architecture and Microprocessor
ssuser034ce1
 
CSN221_Lec_17.pdf Multi Cycle Datapath Design
CSN221_Lec_17.pdf Multi Cycle Datapath DesignCSN221_Lec_17.pdf Multi Cycle Datapath Design
CSN221_Lec_17.pdf Multi Cycle Datapath Design
ssuser034ce1
 
CSN221_Lec_16.pdf MIPS ISA and Datapath design
CSN221_Lec_16.pdf MIPS ISA and Datapath designCSN221_Lec_16.pdf MIPS ISA and Datapath design
CSN221_Lec_16.pdf MIPS ISA and Datapath design
ssuser034ce1
 
CSN221_Lec_15.pdf MIPS ISA and Datapath design
CSN221_Lec_15.pdf MIPS ISA and Datapath designCSN221_Lec_15.pdf MIPS ISA and Datapath design
CSN221_Lec_15.pdf MIPS ISA and Datapath design
ssuser034ce1
 
Computer Architecture CSN221_Lec_37_SpecialTopics.pdf
Computer Architecture CSN221_Lec_37_SpecialTopics.pdfComputer Architecture CSN221_Lec_37_SpecialTopics.pdf
Computer Architecture CSN221_Lec_37_SpecialTopics.pdf
ssuser034ce1
 
CSN221_Lec_5.pdf Computer Organization, CPU Structure and Functions
CSN221_Lec_5.pdf Computer Organization, CPU Structure and FunctionsCSN221_Lec_5.pdf Computer Organization, CPU Structure and Functions
CSN221_Lec_5.pdf Computer Organization, CPU Structure and Functions
ssuser034ce1
 
CSN221_Lec_4.pdf Computer Organization & Architecture
CSN221_Lec_4.pdf Computer Organization & ArchitectureCSN221_Lec_4.pdf Computer Organization & Architecture
CSN221_Lec_4.pdf Computer Organization & Architecture
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.pdf
ssuser034ce1
 
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
ssuser034ce1
 
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
ssuser034ce1
 
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
ssuser034ce1
 
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
ssuser034ce1
 
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
ssuser034ce1
 
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
ssuser034ce1
 
Ad

Recently uploaded (20)

"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Journal of Soft Computing in Civil Engineering
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
The Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLabThe Gaussian Process Modeling Module in UQLab
The Gaussian Process Modeling Module in UQLab
Journal of Soft Computing in Civil Engineering
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G..."Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
"Feed Water Heaters in Thermal Power Plants: Types, Working, and Efficiency G...
Infopitaara
 
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdfRICS Membership-(The Royal Institution of Chartered Surveyors).pdf
RICS Membership-(The Royal Institution of Chartered Surveyors).pdf
MohamedAbdelkader115
 
Smart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineeringSmart Storage Solutions.pptx for production engineering
Smart Storage Solutions.pptx for production engineering
rushikeshnavghare94
 
Metal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistryMetal alkyne complexes.pptx in chemistry
Metal alkyne complexes.pptx in chemistry
mee23nu
 
Oil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdfOil-gas_Unconventional oil and gass_reseviours.pdf
Oil-gas_Unconventional oil and gass_reseviours.pdf
M7md3li2
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
π0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalizationπ0.5: a Vision-Language-Action Model with Open-World Generalization
π0.5: a Vision-Language-Action Model with Open-World Generalization
NABLAS株式会社
 
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdfMAQUINARIA MINAS CEMA 6th Edition (1).pdf
MAQUINARIA MINAS CEMA 6th Edition (1).pdf
ssuser562df4
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
Mathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdfMathematical foundation machine learning.pdf
Mathematical foundation machine learning.pdf
TalhaShahid49
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
IntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdfIntroSlides-April-BuildWithAI-VertexAI.pdf
IntroSlides-April-BuildWithAI-VertexAI.pdf
Luiz Carneiro
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)AI-assisted Software Testing (3-hours tutorial)
AI-assisted Software Testing (3-hours tutorial)
Vəhid Gəruslu
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
Ad

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)