SlideShare a Scribd company logo
CHAPTER 6 1
CHAPTER 6
GRAPHS
All the programs in this file are selected from
Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed
“Fundamentals of Data Structures in C”,
Computer Science Press, 1992.
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
 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 3
Examples for Graph
0
1 2
3
0
1
2
0
1 2
3 4 5 6
G1
G2
G3
V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)}
V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)}
V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>}
complete undirected graph: n(n-1)/2 edges
complete directed graph: n(n-1) edges
complete graph incomplete graph
CHAPTER 6 4
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 graph
CHAPTER 6 5
CHAPTER 6 6
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
CHAPTER 6 7
0 2
1
(a)
2
1
0
3
(b)
*Figure 6.3:Example of a graph with feedback loops and a
multigraph (p.260)
self edge multigraph:
multiple occurrences
of the same edge
Figure 6.3
CHAPTER 6 8
 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)
 A path from vertex vp to vertex vq in a graph G,
is a sequence of vertices, vp, vi1, vi2, ..., vin, vq,
such that (vp, vi1), (vi1, vi2), ..., (vin, vq) are edges
in an undirected graph
 The length of a path is the number of edges on
it
Subgraph and Path
CHAPTER 6 9
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
Figure 6.4: subgraphs of G1 and G3 (p.261)
CHAPTER 6 10
 A simple path is a path in which all vertices,
except possibly the first and the last, are distinct
 A cycle is a simple path in which the first and
the last vertices are the same
 In an undirected graph G, two vertices, v0 and v1,
are connected if there is a path in G from v0 to v1
 An undirected graph is connected if, for every
pair of distinct vertices vi, vj, there is a path
from vi to vj
Simple Path and Style
CHAPTER 6 11
0
1 2
3
0
1 2
3 4 5 6
G1
G2
connected
tree (acyclic graph)
CHAPTER 6 12
Degree
 The degree of a vertex is the number of edges
incident to that vertex
 For directed graph,
– the in-degree of a vertex v is the number of edges
that have v as the head
– the out-degree of a vertex v is the number of edges
that have v as the tail
– if di is the degree of a vertex i in a graph G with n
vertices and e edges, the number of edges is
e di
n


( ) /
0
1
2
CHAPTER 6 13
undirected graph
degree
0
1 2
3 4 5 6
G1 G2
3
2
3 3
1 1 1 1
directed graph
in-degree
out-degree
0
1
2
G3
in:1, out: 1
in: 1, out: 2
in: 1, out: 0
0
1 2
3
33
3
CHAPTER 6 14
Graph Representations
 Adjacency Matrix
 Adjacency List
CHAPTER 6 15
Adjacency Matrix
 Let G=(V,E) be a graph with n vertices.
 The adjacency matrix of G is a two-dimensional
n by n array, say adj_mat
 If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
 If there is no such edge in E(G), adj_mat[i][j]=0
 The adjacency matrix for an undirected graph is
symmetric; the adjacency matrix for a digraph
need not be symmetric
CHAPTER 6 16
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
0
1 2
3
0
1
2
1
0
2
3
4
5
6
7
symmetric
undirected: n2/2
directed: n2
CHAPTER 6 17
Data Structures for Adjacency Lists
#define MAX_VERTICES 50
typedef struct node *node_pointer;
typedef struct node {
int vertex;
struct node *link;
};
node_pointer graph[MAX_VERTICES];
int n=0; /* vertices currently in use */
Each row in adjacency matrix is represented as an adjacency list.
CHAPTER 6 18
0
1
2
3
0
1
2
0
1
2
3
4
5
6
7
1 2 3
0 2 3
0 1 3
0 1 2
G1
1
0 2
G3
1 2
0 3
0 3
1 2
5
4 6
5 7
6
G4
0
1 2
3
0
1
2
1
0
2
3
4
5
6
7
An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
CHAPTER 6 19
Some Graph Operations
 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
David Luebke
Review: Breadth-First Search
 “Explore” a graph, turning it into a tree
– One vertex at a time
– Expand frontier of explored vertices across the
breadth of the frontier
 Builds a tree over the graph
– Pick a source vertex to be the root
– Find (“discover”) its children, then their
children, etc.
David Luebke
Review: Breadth-First Search
 Again will associate vertex “colors” to
guide the algorithm
– White vertices have not been discovered
• All vertices start out white
– Grey vertices are discovered but not fully
explored
• They may be adjacent to white vertices
– Black vertices are discovered and fully
explored
• They are adjacent only to black and gray vertices
David Luebke
Depth-First Search
 Depth-first search is another strategy for
exploring a graph
– Explore “deeper” in the graph whenever
possible
– Edges are explored out of the most recently
discovered vertex v that still has unexplored
edges
– When all of v’s edges have been explored,
backtrack to the vertex from which v was
discovered

More Related Content

What's hot

Graph theory presentation
Graph theory presentationGraph theory presentation
Graph theory presentation
Aliul Kadir Akib
 
Graph theory
Graph theoryGraph theory
Graph theory
Jeane Paguio
 
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
nabati
 
Graph theory introduction - Samy
Graph theory  introduction - SamyGraph theory  introduction - Samy
Graph theory introduction - Samy
Mark Arokiasamy
 
Graph theory
Graph theoryGraph theory
Graph theory
Nitesh Nayal
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Saurabh Kaushik
 
Graph theory
Graph theoryGraph theory
Graph theory
manikanta361
 
Graphs - Discrete Math
Graphs - Discrete MathGraphs - Discrete Math
Graphs - Discrete Math
Sikder Tahsin Al-Amin
 
Graph theory
Graph theoryGraph theory
Graph theory
Gaditek
 
Bn31231239
Bn31231239Bn31231239
Bn31231239
IJMER
 
Graphs
GraphsGraphs
Graph theory
Graph theoryGraph theory
Graph theory
Lifeparticle
 
Graphs
GraphsGraphs
Graphs
Mohd Arif
 
Graph
GraphGraph
Graph theory 1
Graph theory 1Graph theory 1
Graph theory 1
Tech_MX
 
Ppt of graph theory
Ppt of graph theoryPpt of graph theory
Ppt of graph theory
ArvindBorge
 
1452 86301000013 m
1452 86301000013 m1452 86301000013 m
1452 86301000013 m
Praveen Kumar
 
Graphs Algorithms
Graphs AlgorithmsGraphs Algorithms
Graphs Algorithms
Kasun Ranga Wijeweera
 

What's hot (18)

Graph theory presentation
Graph theory presentationGraph theory presentation
Graph theory presentation
 
Graph theory
Graph theoryGraph theory
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
 
Graph theory introduction - Samy
Graph theory  introduction - SamyGraph theory  introduction - Samy
Graph theory introduction - Samy
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph ColouringGraph Theory,Graph Terminologies,Planar Graph & Graph Colouring
Graph Theory,Graph Terminologies,Planar Graph & Graph Colouring
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Graphs - Discrete Math
Graphs - Discrete MathGraphs - Discrete Math
Graphs - Discrete Math
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Bn31231239
Bn31231239Bn31231239
Bn31231239
 
Graphs
GraphsGraphs
Graphs
 
Graph theory
Graph theoryGraph theory
Graph theory
 
Graphs
GraphsGraphs
Graphs
 
Graph
GraphGraph
Graph
 
Graph theory 1
Graph theory 1Graph theory 1
Graph theory 1
 
Ppt of graph theory
Ppt of graph theoryPpt of graph theory
Ppt of graph theory
 
1452 86301000013 m
1452 86301000013 m1452 86301000013 m
1452 86301000013 m
 
Graphs Algorithms
Graphs AlgorithmsGraphs Algorithms
Graphs Algorithms
 

Similar to Grpahs in Data Structure

Dsa.PPT
Dsa.PPTDsa.PPT
Dsa.PPT
boltkat
 
chapter6.PPT
chapter6.PPTchapter6.PPT
chapter6.PPT
krish448427
 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6
DrkhanchanaR
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
DEEPAK948083
 
Chap 6 Graph.ppt
Chap 6 Graph.pptChap 6 Graph.ppt
Chap 6 Graph.ppt
shashankbhadouria4
 
09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf
Prasanna David
 
Graph in Data Structure
Graph in Data StructureGraph in Data Structure
Graph in Data Structure
Prof Ansari
 
Discrete maths assignment
Discrete maths assignmentDiscrete maths assignment
Discrete maths assignment
Keshav Somani
 
Cs6702 2marks rejinpaul
Cs6702 2marks rejinpaulCs6702 2marks rejinpaul
Cs6702 2marks rejinpaul
stalinjothi
 
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
appasami
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
Madhu Bala
 
Graphs
GraphsGraphs
Graphs
amudha arul
 
Graph
GraphGraph
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
Savit Chandra
 
Graph-theory.ppt
Graph-theory.pptGraph-theory.ppt
Graph-theory.ppt
AlpaSinghRajput1
 
Graph.ppt
Graph.pptGraph.ppt
Graph.ppt
AlpaSinghRajput1
 
Lecture 1--Graph Algorithms -- Basics.pptx
Lecture 1--Graph Algorithms -- Basics.pptxLecture 1--Graph Algorithms -- Basics.pptx
Lecture 1--Graph Algorithms -- Basics.pptx
ChandanGiri21
 
graph.ppt
graph.pptgraph.ppt
graph theory
graph theorygraph theory
graph theory
Shashank Singh
 
Matrix representation of graph
Matrix representation of graphMatrix representation of graph
Matrix representation of graph
Rounak Biswas
 

Similar to Grpahs in Data Structure (20)

Dsa.PPT
Dsa.PPTDsa.PPT
Dsa.PPT
 
chapter6.PPT
chapter6.PPTchapter6.PPT
chapter6.PPT
 
Unit 3 graph chapter6
Unit 3  graph chapter6Unit 3  graph chapter6
Unit 3 graph chapter6
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
Chap 6 Graph.ppt
Chap 6 Graph.pptChap 6 Graph.ppt
Chap 6 Graph.ppt
 
09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf09_DS_MCA_Graphs.pdf
09_DS_MCA_Graphs.pdf
 
Graph in Data Structure
Graph in Data StructureGraph in Data Structure
Graph in Data Structure
 
Discrete maths assignment
Discrete maths assignmentDiscrete maths assignment
Discrete maths assignment
 
Cs6702 2marks rejinpaul
Cs6702 2marks rejinpaulCs6702 2marks rejinpaul
Cs6702 2marks rejinpaul
 
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
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Graphs
GraphsGraphs
Graphs
 
Graph
GraphGraph
Graph
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
Graph-theory.ppt
Graph-theory.pptGraph-theory.ppt
Graph-theory.ppt
 
Graph.ppt
Graph.pptGraph.ppt
Graph.ppt
 
Lecture 1--Graph Algorithms -- Basics.pptx
Lecture 1--Graph Algorithms -- Basics.pptxLecture 1--Graph Algorithms -- Basics.pptx
Lecture 1--Graph Algorithms -- Basics.pptx
 
graph.ppt
graph.pptgraph.ppt
graph.ppt
 
graph theory
graph theorygraph theory
graph theory
 
Matrix representation of graph
Matrix representation of graphMatrix representation of graph
Matrix representation of graph
 

More from AvichalVishnoi

Tower of hanoi
Tower of hanoiTower of hanoi
Tower of hanoi
AvichalVishnoi
 
B trees
B treesB trees
Bfs dfs mst
Bfs dfs mstBfs dfs mst
Bfs dfs mst
AvichalVishnoi
 
Dsa sorting
Dsa sortingDsa sorting
Dsa sorting
AvichalVishnoi
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
AvichalVishnoi
 
Pointer in c
Pointer in cPointer in c
Pointer in c
AvichalVishnoi
 

More from AvichalVishnoi (6)

Tower of hanoi
Tower of hanoiTower of hanoi
Tower of hanoi
 
B trees
B treesB trees
B trees
 
Bfs dfs mst
Bfs dfs mstBfs dfs mst
Bfs dfs mst
 
Dsa sorting
Dsa sortingDsa sorting
Dsa sorting
 
Propositional logic
Propositional logicPropositional logic
Propositional logic
 
Pointer in c
Pointer in cPointer in c
Pointer in c
 

Recently uploaded

The Ipsos - AI - Monitor 2024 Report.pdf
The  Ipsos - AI - Monitor 2024 Report.pdfThe  Ipsos - AI - Monitor 2024 Report.pdf
The Ipsos - AI - Monitor 2024 Report.pdf
Social Samosa
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
apvysm8
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
Timothy Spann
 
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging DataPredictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Kiwi Creative
 
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Aggregage
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
rwarrenll
 
Experts live - Improving user adoption with AI
Experts live - Improving user adoption with AIExperts live - Improving user adoption with AI
Experts live - Improving user adoption with AI
jitskeb
 
A presentation that explain the Power BI Licensing
A presentation that explain the Power BI LicensingA presentation that explain the Power BI Licensing
A presentation that explain the Power BI Licensing
AlessioFois2
 
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
74nqk8xf
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
manishkhaire30
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
Sm321
 
The Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series DatabaseThe Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series Database
javier ramirez
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
aqzctr7x
 
End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024
Lars Albertsson
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
74nqk8xf
 
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
g4dpvqap0
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
v7oacc3l
 
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
nuttdpt
 
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdfUdemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Fernanda Palhano
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
bopyb
 

Recently uploaded (20)

The Ipsos - AI - Monitor 2024 Report.pdf
The  Ipsos - AI - Monitor 2024 Report.pdfThe  Ipsos - AI - Monitor 2024 Report.pdf
The Ipsos - AI - Monitor 2024 Report.pdf
 
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
办(uts毕业证书)悉尼科技大学毕业证学历证书原版一模一样
 
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
06-04-2024 - NYC Tech Week - Discussion on Vector Databases, Unstructured Dat...
 
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging DataPredictably Improve Your B2B Tech Company's Performance by Leveraging Data
Predictably Improve Your B2B Tech Company's Performance by Leveraging Data
 
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
Beyond the Basics of A/B Tests: Highly Innovative Experimentation Tactics You...
 
My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.My burning issue is homelessness K.C.M.O.
My burning issue is homelessness K.C.M.O.
 
Experts live - Improving user adoption with AI
Experts live - Improving user adoption with AIExperts live - Improving user adoption with AI
Experts live - Improving user adoption with AI
 
A presentation that explain the Power BI Licensing
A presentation that explain the Power BI LicensingA presentation that explain the Power BI Licensing
A presentation that explain the Power BI Licensing
 
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
一比一原版(Chester毕业证书)切斯特大学毕业证如何办理
 
Learn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queriesLearn SQL from basic queries to Advance queries
Learn SQL from basic queries to Advance queries
 
Challenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more importantChallenges of Nation Building-1.pptx with more important
Challenges of Nation Building-1.pptx with more important
 
The Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series DatabaseThe Building Blocks of QuestDB, a Time Series Database
The Building Blocks of QuestDB, a Time Series Database
 
一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理一比一原版(UO毕业证)渥太华大学毕业证如何办理
一比一原版(UO毕业证)渥太华大学毕业证如何办理
 
End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024End-to-end pipeline agility - Berlin Buzzwords 2024
End-to-end pipeline agility - Berlin Buzzwords 2024
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
 
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
一比一原版(Glasgow毕业证书)格拉斯哥大学毕业证如何办理
 
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
在线办理(英国UCA毕业证书)创意艺术大学毕业证在读证明一模一样
 
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
一比一原版(UCSB文凭证书)圣芭芭拉分校毕业证如何办理
 
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdfUdemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
Udemy_2024_Global_Learning_Skills_Trends_Report (1).pdf
 
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
一比一原版(GWU,GW文凭证书)乔治·华盛顿大学毕业证如何办理
 

Grpahs in Data Structure

  • 1. CHAPTER 6 1 CHAPTER 6 GRAPHS All the programs in this file are selected from Ellis Horowitz, Sartaj Sahni, and Susan Anderson-Freed “Fundamentals of Data Structures in C”, Computer Science Press, 1992.
  • 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  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
  • 3. CHAPTER 6 3 Examples for Graph 0 1 2 3 0 1 2 0 1 2 3 4 5 6 G1 G2 G3 V(G1)={0,1,2,3} E(G1)={(0,1),(0,2),(0,3),(1,2),(1,3),(2,3)} V(G2)={0,1,2,3,4,5,6} E(G2)={(0,1),(0,2),(1,3),(1,4),(2,5),(2,6)} V(G3)={0,1,2} E(G3)={<0,1>,<1,0>,<1,2>} complete undirected graph: n(n-1)/2 edges complete directed graph: n(n-1) edges complete graph incomplete graph
  • 4. CHAPTER 6 4 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 graph
  • 6. CHAPTER 6 6 Adjacent and Incident  If (v0, v1) is an edge in an undirected graph, – v0 and v1 are adjacent – The edge (v0, v1) is incident on vertices v0 and v1  If <v0, v1> is an edge in a directed graph – v0 is adjacent to v1, and v1 is adjacent from v0 – The edge <v0, v1> is incident on v0 and v1
  • 7. CHAPTER 6 7 0 2 1 (a) 2 1 0 3 (b) *Figure 6.3:Example of a graph with feedback loops and a multigraph (p.260) self edge multigraph: multiple occurrences of the same edge Figure 6.3
  • 8. CHAPTER 6 8  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)  A path from vertex vp to vertex vq in a graph G, is a sequence of vertices, vp, vi1, vi2, ..., vin, vq, such that (vp, vi1), (vi1, vi2), ..., (vin, vq) are edges in an undirected graph  The length of a path is the number of edges on it Subgraph and Path
  • 9. CHAPTER 6 9 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 Figure 6.4: subgraphs of G1 and G3 (p.261)
  • 10. CHAPTER 6 10  A simple path is a path in which all vertices, except possibly the first and the last, are distinct  A cycle is a simple path in which the first and the last vertices are the same  In an undirected graph G, two vertices, v0 and v1, are connected if there is a path in G from v0 to v1  An undirected graph is connected if, for every pair of distinct vertices vi, vj, there is a path from vi to vj Simple Path and Style
  • 11. CHAPTER 6 11 0 1 2 3 0 1 2 3 4 5 6 G1 G2 connected tree (acyclic graph)
  • 12. CHAPTER 6 12 Degree  The degree of a vertex is the number of edges incident to that vertex  For directed graph, – the in-degree of a vertex v is the number of edges that have v as the head – the out-degree of a vertex v is the number of edges that have v as the tail – if di is the degree of a vertex i in a graph G with n vertices and e edges, the number of edges is e di n   ( ) / 0 1 2
  • 13. CHAPTER 6 13 undirected graph degree 0 1 2 3 4 5 6 G1 G2 3 2 3 3 1 1 1 1 directed graph in-degree out-degree 0 1 2 G3 in:1, out: 1 in: 1, out: 2 in: 1, out: 0 0 1 2 3 33 3
  • 14. CHAPTER 6 14 Graph Representations  Adjacency Matrix  Adjacency List
  • 15. CHAPTER 6 15 Adjacency Matrix  Let G=(V,E) be a graph with n vertices.  The adjacency matrix of G is a two-dimensional n by n array, say adj_mat  If the edge (vi, vj) is in E(G), adj_mat[i][j]=1  If there is no such edge in E(G), adj_mat[i][j]=0  The adjacency matrix for an undirected graph is symmetric; the adjacency matrix for a digraph need not be symmetric
  • 16. CHAPTER 6 16 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 0 1 2 3 0 1 2 1 0 2 3 4 5 6 7 symmetric undirected: n2/2 directed: n2
  • 17. CHAPTER 6 17 Data Structures for Adjacency Lists #define MAX_VERTICES 50 typedef struct node *node_pointer; typedef struct node { int vertex; struct node *link; }; node_pointer graph[MAX_VERTICES]; int n=0; /* vertices currently in use */ Each row in adjacency matrix is represented as an adjacency list.
  • 18. CHAPTER 6 18 0 1 2 3 0 1 2 0 1 2 3 4 5 6 7 1 2 3 0 2 3 0 1 3 0 1 2 G1 1 0 2 G3 1 2 0 3 0 3 1 2 5 4 6 5 7 6 G4 0 1 2 3 0 1 2 1 0 2 3 4 5 6 7 An undirected graph with n vertices and e edges ==> n head nodes and 2e list nodes
  • 19. CHAPTER 6 19 Some Graph Operations  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
  • 20. David Luebke Review: Breadth-First Search  “Explore” a graph, turning it into a tree – One vertex at a time – Expand frontier of explored vertices across the breadth of the frontier  Builds a tree over the graph – Pick a source vertex to be the root – Find (“discover”) its children, then their children, etc.
  • 21. David Luebke Review: Breadth-First Search  Again will associate vertex “colors” to guide the algorithm – White vertices have not been discovered • All vertices start out white – Grey vertices are discovered but not fully explored • They may be adjacent to white vertices – Black vertices are discovered and fully explored • They are adjacent only to black and gray vertices
  • 22. David Luebke Depth-First Search  Depth-first search is another strategy for exploring a graph – Explore “deeper” in the graph whenever possible – Edges are explored out of the most recently discovered vertex v that still has unexplored edges – When all of v’s edges have been explored, backtrack to the vertex from which v was discovered