SlideShare a Scribd company logo
GRAPH
What is a Graph?
• A graph G = (V,E) is composed of:
V: set of vertices
E: set of edges connecting the vertices in V
• An edge e = (u,v) is a pair of vertices
• Example:
a b
c
d e
V= {a,b,c,d,e}
E= {(a,b),(a,c),
(a,d),
(b,e),(c,d),(c,e),
(d,e)}
Applications
• electronic circuits
• networks (roads, flights, communications)
CS16
LAX
JFK
LAX
DFW
STL
HNL
FTL
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
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
Terminology:
Degree of a Vertex
Why? Since adjacent vertices each
count the adjoining edge, it will be
counted twice
0
1 2
3 4 5 6
G1 G2
3 2
3 3
1 1 1 1
directed graph
in-degree
out-degree
0
1
2
G3
in:1, out: 1
in: 1, out: 2
in: 1, out: 0
0
1 2
3
33
3
Examples
Terminology:
Path
• path: sequence of
vertices v1,v2,. . .vk such
that consecutive vertices vi
and vi+1 are adjacent.
7
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
Even More Terminology
• subgraph: subset of vertices and edges forming a graph
• connected component: maximal connected subgraph. E.g., the graph below has
3 connected components.
connected not connected
•connected graph: any two vertices are connected by some path
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
More…
• tree - connected graph without cycles
• forest - collection of trees
tree
forest
tree
tree
tree
Connectivity
• 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
n = 5
m = (5 ∗ 4)/2 = 10
More Connectivity
n = #vertices
m = #edges
• For a tree m = n - 1
n = 5
m = 4
n = 5
m = 3
If m < n - 1, G is
not connected
Oriented (Directed) Graph
• A graph where edges are directed
Directed vs. Undirected 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
ADT for Graph
objects: a nonempty set of vertices and a set of undirected edges, where each
edge is a pair of vertices
functions: for all graph ∈ Graph, v, v1 and v2 ∈ Vertices
Graph Create()::=return an empty graph
Graph InsertVertex(graph, v)::= return a graph with v inserted. v has no
incident edge.
Graph InsertEdge(graph, v1,v2)::= return a graph with new edge
between v1 and v2
Graph DeleteVertex(graph, v)::= return a graph in which v and all edges
incident to it are removed
Graph DeleteEdge(graph, v1, v2)::=return a graph in which the edge (v1, v2)
is removed
Boolean IsEmpty(graph)::= if (graph==empty graph) return TRUE
else return FALSE
List Adjacent(graph,v)::= return a list of all vertices that are adjacent to v
Graph Representations
Adjacency Matrix
Adjacency Lists
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
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
Merits of Adjacency Matrix
From the adjacency matrix, to determine the
connection of vertices is easy
The degree of a vertex is
For a digraph (= directed graph), the row sum is
the out_degree, while the column sum is the
in_degree
adj mat i j
j
n
_ [ ][ ]
=
−
∑0
1
ind vi A j i
j
n
( ) [ , ]=
=
−
∑
0
1
outd vi A i j
j
n
( ) [ , ]=
=
−
∑
0
1
Adjacency Lists (data structures)
#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.
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
Some Operations
degree of a vertex in an undirected graph
–# of nodes in adjacency list
# of edges in a graph
–determined in O(n+e)
out-degree of a vertex in a directed graph
–# of nodes in its adjacency list
in-degree of a vertex in a directed graph
–traverse the whole data structure
Graph Traversal
• Problem: Search for a certain node or traverse
all nodes in the graph
• Depth First Search
– Once a possible path is found, continue the search
until the end of the path
• Breadth First Search
– Start several paths at a time, and advance in each
one step at a time
Depth-First Search
M N O P
I J K L
E F G H
A B C D
Exploring a Labyrinth
Without Getting Lost
• A depth-first search (DFS) in an undirected graph G is like wandering
in a labyrinth with a string and a can of red paint without getting lost.
• We start at vertex s, tying the end of our string to the point and painting
s “visited”. Next we label s as our current vertex called u.
• Now we travel along an arbitrary edge (u, v).
• If edge (u, v) leads us to an already visited vertex v we return to u.
• If vertex v is unvisited, we unroll our string and move to v, paint v
“visited”, set v as our current vertex, and repeat the previous steps.
Breadth-First Search
• Like DFS, a Breadth-First Search (BFS) traverses a connected component of a
graph, and in doing so defines a spanning tree with several useful properties.
• The starting vertex s has level 0, and, as in DFS, defines that point as an
“anchor.”
• In the first round, the string is unrolled the length of one edge, and all of the
edges that are only one edge away from the anchor are visited.
• These edges are placed into level 1
• In the second round, all the new edges that can be reached by unrolling the
string 2 edges are visited and placed in level 2.
• This continues until every vertex has been assigned a level.
• The label of any vertex v corresponds to the length of the shortest path from s
to v.
27
BFS - A Graphical Representation
M N O P
I J K L
E F G H
A B C D
0
M N O P
I J K L
E F G H
A B C D
0 1
28M N O P
I J K L
E F G H
A C DB
0 1 2
M N O P
I J K L
E F G H
A B C D
0 1 2 3
d)c)
b)a)
More BFS
M N O P
I J K L
E F G H
A B C D
4
0 1 2 3
M N O P
I J K L
E F G H
A B C D
4
5
0 1 2 3
Applications: Finding a Path
• Find path from source vertex s to destination
vertex d
• Use graph search starting at s and terminating as
soon as we reach d
– Need to remember edges traversed
• Use depth – first search ?
• Use breath – first search?
DFS vs. BFS
E
F
G
B
CD
A start
destination
A DFS on A A
DFS on BB
A
DFS on C
B
C
A
B Return to call on B
D Call DFS on D
A
B
D
Call DFS on GG found destination - done!
Path is implicitly stored in DFS recursion
Path is: A, B, D, G
DFS Process
DFS vs. BFS
E
F
G
B
CD
A start
destination
BFS Process
A
Initial call to BFS on A
Add A to queue
B
Dequeue A
Add B
frontrear frontrear
C
Dequeue B
Add C, D
frontrear
D D
Dequeue C
Nothing to add
frontrear
G
Dequeue D
Add G
frontrear
found destination - done!
Path must be stored separately

More Related Content

What's hot

Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
Abrish06
 
Array data structure
Array data structureArray data structure
Array data structure
maamir farooq
 
Data structures & algorithms lecture 3
Data structures & algorithms lecture 3Data structures & algorithms lecture 3
Data structures & algorithms lecture 3
Poojith Chowdhary
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
Intro C# Book
 
Graphs data structures
Graphs data structuresGraphs data structures
Graphs data structures
Jasleen Kaur (Chandigarh University)
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
DEEPIKA T
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
shameen khan
 
Tree-In Data Structure
Tree-In Data StructureTree-In Data Structure
Tree-In Data Structure
United International University
 
Application of dfs
Application of dfsApplication of dfs
Application of dfs
Hossain Md Shakhawat
 
Lecture8 data structure(graph)
Lecture8 data structure(graph)Lecture8 data structure(graph)
Graphs in c language
Graphs in c languageGraphs in c language
Graphs in c language
SARITHA REDDY
 
Algorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsAlgorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph Algorithms
Mohamed Loey
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithm
hansa khan
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of treeSiddhi Viradiya
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
Kevin Jadiya
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
Krish_ver2
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
bca2010
 
Sequential pattern mining
Sequential pattern miningSequential pattern mining
Sequential pattern miningkiran said
 

What's hot (20)

Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Array data structure
Array data structureArray data structure
Array data structure
 
Data structures & algorithms lecture 3
Data structures & algorithms lecture 3Data structures & algorithms lecture 3
Data structures & algorithms lecture 3
 
17. Trees and Graphs
17. Trees and Graphs17. Trees and Graphs
17. Trees and Graphs
 
Graphs data structures
Graphs data structuresGraphs data structures
Graphs data structures
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
DFS & BFS Graph
DFS & BFS GraphDFS & BFS Graph
DFS & BFS Graph
 
Tree-In Data Structure
Tree-In Data StructureTree-In Data Structure
Tree-In Data Structure
 
Application of dfs
Application of dfsApplication of dfs
Application of dfs
 
Lecture8 data structure(graph)
Lecture8 data structure(graph)Lecture8 data structure(graph)
Lecture8 data structure(graph)
 
Graphs in c language
Graphs in c languageGraphs in c language
Graphs in c language
 
Algorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph AlgorithmsAlgorithms Lecture 7: Graph Algorithms
Algorithms Lecture 7: Graph Algorithms
 
Graphs
GraphsGraphs
Graphs
 
Depth firstsearchalgorithm
Depth firstsearchalgorithmDepth firstsearchalgorithm
Depth firstsearchalgorithm
 
non linear data structure -introduction of tree
non linear data structure -introduction of treenon linear data structure -introduction of tree
non linear data structure -introduction of tree
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Sequential pattern mining
Sequential pattern miningSequential pattern mining
Sequential pattern mining
 

Viewers also liked

Graphss
GraphssGraphss
Graphss
fika sweety
 
Data Structures, Graphs
Data Structures, GraphsData Structures, Graphs
Data Structures, GraphsJibrael Jos
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
Prabhu Govind
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphsKumar
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
 
Matrix Representation Of Graph
Matrix Representation Of GraphMatrix Representation Of Graph
Matrix Representation Of GraphAbhishek Pachisia
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
Ankit Kumar Singh
 

Viewers also liked (7)

Graphss
GraphssGraphss
Graphss
 
Data Structures, Graphs
Data Structures, GraphsData Structures, Graphs
Data Structures, Graphs
 
Memory allocation in c
Memory allocation in cMemory allocation in c
Memory allocation in c
 
Data structure computer graphs
Data structure computer graphsData structure computer graphs
Data structure computer graphs
 
Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]Data Structures - Lecture 10 [Graphs]
Data Structures - Lecture 10 [Graphs]
 
Matrix Representation Of Graph
Matrix Representation Of GraphMatrix Representation Of Graph
Matrix Representation Of Graph
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
 

Similar to Graphs in data structures

Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
ChristianKapsales1
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
hijigaf
 
Graphs
GraphsGraphs
Graphs
amudha arul
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data StructureAnuj Modi
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsAakash deep Singhal
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashingVictor Palmar
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
Dabbal Singh Mahara
 
Graph
GraphGraph
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
ARVIND SARDAR
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
Tribhuvan University
 
Graphs
GraphsGraphs
Breadth first search
Breadth first searchBreadth first search
Breadth first search
Sazzad Hossain
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
Rashmi Bhat
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
Hector Zenil
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
ARVIND SARDAR
 
Chap 6 Graph.ppt
Chap 6 Graph.pptChap 6 Graph.ppt
Chap 6 Graph.ppt
shashankbhadouria4
 
Graph therory
Graph theroryGraph therory
Graph therory
mohanrathod18
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
zerihunnana
 

Similar to Graphs in data structures (20)

Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdfClass01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
Class01_Computer_Contest_Level_3_Notes_Sep_07 - Copy.pdf
 
10.graph
10.graph10.graph
10.graph
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
Graphs
GraphsGraphs
Graphs
 
Graphs In Data Structure
Graphs In Data StructureGraphs In Data Structure
Graphs In Data Structure
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
Unit 9 graph
Unit   9 graphUnit   9 graph
Unit 9 graph
 
Graph
GraphGraph
Graph
 
graph ASS (1).ppt
graph ASS (1).pptgraph ASS (1).ppt
graph ASS (1).ppt
 
Unit ix graph
Unit   ix    graph Unit   ix    graph
Unit ix graph
 
Graphs
GraphsGraphs
Graphs
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Graph Theory
Graph TheoryGraph Theory
Graph Theory
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
Unit 2: All
Unit 2: AllUnit 2: All
Unit 2: All
 
Graph ASS DBATU.pptx
Graph ASS DBATU.pptxGraph ASS DBATU.pptx
Graph ASS DBATU.pptx
 
Chap 6 Graph.ppt
Chap 6 Graph.pptChap 6 Graph.ppt
Chap 6 Graph.ppt
 
Graph therory
Graph theroryGraph therory
Graph therory
 
Algorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptxAlgorithms and data Chapter 3 V Graph.pptx
Algorithms and data Chapter 3 V Graph.pptx
 

Recently uploaded

Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
benishzehra469
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
ArpitMalhotra16
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
dwreak4tg
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
jerlynmaetalle
 
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
g4dpvqap0
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
Oppotus
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
ocavb
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
jerlynmaetalle
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
ewymefz
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Subhajit Sahu
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
AbhimanyuSinha9
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Boston Institute of Analytics
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
74nqk8xf
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
pchutichetpong
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
slg6lamcq
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
John Andrews
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
v3tuleee
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
enxupq
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
balafet
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
yhkoc
 

Recently uploaded (20)

Empowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptxEmpowering Data Analytics Ecosystem.pptx
Empowering Data Analytics Ecosystem.pptx
 
standardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghhstandardisation of garbhpala offhgfffghh
standardisation of garbhpala offhgfffghh
 
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
一比一原版(BCU毕业证书)伯明翰城市大学毕业证如何办理
 
The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...The affect of service quality and online reviews on customer loyalty in the E...
The affect of service quality and online reviews on customer loyalty in the E...
 
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
一比一原版(爱大毕业证书)爱丁堡大学毕业证如何办理
 
Q1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year ReboundQ1’2024 Update: MYCI’s Leap Year Rebound
Q1’2024 Update: MYCI’s Leap Year Rebound
 
一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单一比一原版(TWU毕业证)西三一大学毕业证成绩单
一比一原版(TWU毕业证)西三一大学毕业证成绩单
 
Influence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business PlanInfluence of Marketing Strategy and Market Competition on Business Plan
Influence of Marketing Strategy and Market Competition on Business Plan
 
一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单一比一原版(NYU毕业证)纽约大学毕业证成绩单
一比一原版(NYU毕业证)纽约大学毕业证成绩单
 
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
Levelwise PageRank with Loop-Based Dead End Handling Strategy : SHORT REPORT ...
 
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...Best best suvichar in gujarati english meaning of this sentence as Silk road ...
Best best suvichar in gujarati english meaning of this sentence as Silk road ...
 
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project PresentationPredicting Product Ad Campaign Performance: A Data Analysis Project Presentation
Predicting Product Ad Campaign Performance: A Data Analysis Project Presentation
 
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
一比一原版(Coventry毕业证书)考文垂大学毕业证如何办理
 
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
Data Centers - Striving Within A Narrow Range - Research Report - MCG - May 2...
 
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
一比一原版(Adelaide毕业证书)阿德莱德大学毕业证如何办理
 
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
Chatty Kathy - UNC Bootcamp Final Project Presentation - Final Version - 5.23...
 
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理一比一原版(UofS毕业证书)萨省大学毕业证如何办理
一比一原版(UofS毕业证书)萨省大学毕业证如何办理
 
一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单一比一原版(YU毕业证)约克大学毕业证成绩单
一比一原版(YU毕业证)约克大学毕业证成绩单
 
Machine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptxMachine learning and optimization techniques for electrical drives.pptx
Machine learning and optimization techniques for electrical drives.pptx
 
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
一比一原版(CU毕业证)卡尔顿大学毕业证成绩单
 

Graphs in data structures

  • 2. What is a Graph? • A graph G = (V,E) is composed of: V: set of vertices E: set of edges connecting the vertices in V • An edge e = (u,v) is a pair of vertices • Example: a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c), (a,d), (b,e),(c,d),(c,e), (d,e)}
  • 3. Applications • electronic circuits • networks (roads, flights, communications) CS16 LAX JFK LAX DFW STL HNL FTL
  • 4. 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
  • 5. 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 Terminology: Degree of a Vertex Why? Since adjacent vertices each count the adjoining edge, it will be counted twice
  • 6. 0 1 2 3 4 5 6 G1 G2 3 2 3 3 1 1 1 1 directed graph in-degree out-degree 0 1 2 G3 in:1, out: 1 in: 1, out: 2 in: 1, out: 0 0 1 2 3 33 3 Examples
  • 7. Terminology: Path • path: sequence of vertices v1,v2,. . .vk such that consecutive vertices vi and vi+1 are adjacent. 7 3 3 3 3 2 a b c d e a b c d e a b e d c b e d c
  • 8. 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
  • 9. Even More Terminology • subgraph: subset of vertices and edges forming a graph • connected component: maximal connected subgraph. E.g., the graph below has 3 connected components. connected not connected •connected graph: any two vertices are connected by some path
  • 10. 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
  • 11. More… • tree - connected graph without cycles • forest - collection of trees tree forest tree tree tree
  • 12. Connectivity • 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 n = 5 m = (5 ∗ 4)/2 = 10
  • 13. More Connectivity n = #vertices m = #edges • For a tree m = n - 1 n = 5 m = 4 n = 5 m = 3 If m < n - 1, G is not connected
  • 14. Oriented (Directed) Graph • A graph where edges are directed
  • 15. Directed vs. Undirected 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
  • 16. ADT for Graph objects: a nonempty set of vertices and a set of undirected edges, where each edge is a pair of vertices functions: for all graph ∈ Graph, v, v1 and v2 ∈ Vertices Graph Create()::=return an empty graph Graph InsertVertex(graph, v)::= return a graph with v inserted. v has no incident edge. Graph InsertEdge(graph, v1,v2)::= return a graph with new edge between v1 and v2 Graph DeleteVertex(graph, v)::= return a graph in which v and all edges incident to it are removed Graph DeleteEdge(graph, v1, v2)::=return a graph in which the edge (v1, v2) is removed Boolean IsEmpty(graph)::= if (graph==empty graph) return TRUE else return FALSE List Adjacent(graph,v)::= return a list of all vertices that are adjacent to v
  • 18. 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
  • 19. 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
  • 20. Merits of Adjacency Matrix From the adjacency matrix, to determine the connection of vertices is easy The degree of a vertex is For a digraph (= directed graph), the row sum is the out_degree, while the column sum is the in_degree adj mat i j j n _ [ ][ ] = − ∑0 1 ind vi A j i j n ( ) [ , ]= = − ∑ 0 1 outd vi A i j j n ( ) [ , ]= = − ∑ 0 1
  • 21. Adjacency Lists (data structures) #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.
  • 22. 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
  • 23. Some Operations degree of a vertex in an undirected graph –# of nodes in adjacency list # of edges in a graph –determined in O(n+e) out-degree of a vertex in a directed graph –# of nodes in its adjacency list in-degree of a vertex in a directed graph –traverse the whole data structure
  • 24. Graph Traversal • Problem: Search for a certain node or traverse all nodes in the graph • Depth First Search – Once a possible path is found, continue the search until the end of the path • Breadth First Search – Start several paths at a time, and advance in each one step at a time
  • 25. Depth-First Search M N O P I J K L E F G H A B C D
  • 26. Exploring a Labyrinth Without Getting Lost • A depth-first search (DFS) in an undirected graph G is like wandering in a labyrinth with a string and a can of red paint without getting lost. • We start at vertex s, tying the end of our string to the point and painting s “visited”. Next we label s as our current vertex called u. • Now we travel along an arbitrary edge (u, v). • If edge (u, v) leads us to an already visited vertex v we return to u. • If vertex v is unvisited, we unroll our string and move to v, paint v “visited”, set v as our current vertex, and repeat the previous steps.
  • 27. Breadth-First Search • Like DFS, a Breadth-First Search (BFS) traverses a connected component of a graph, and in doing so defines a spanning tree with several useful properties. • The starting vertex s has level 0, and, as in DFS, defines that point as an “anchor.” • In the first round, the string is unrolled the length of one edge, and all of the edges that are only one edge away from the anchor are visited. • These edges are placed into level 1 • In the second round, all the new edges that can be reached by unrolling the string 2 edges are visited and placed in level 2. • This continues until every vertex has been assigned a level. • The label of any vertex v corresponds to the length of the shortest path from s to v. 27
  • 28. BFS - A Graphical Representation M N O P I J K L E F G H A B C D 0 M N O P I J K L E F G H A B C D 0 1 28M N O P I J K L E F G H A C DB 0 1 2 M N O P I J K L E F G H A B C D 0 1 2 3 d)c) b)a)
  • 29. More BFS M N O P I J K L E F G H A B C D 4 0 1 2 3 M N O P I J K L E F G H A B C D 4 5 0 1 2 3
  • 30. Applications: Finding a Path • Find path from source vertex s to destination vertex d • Use graph search starting at s and terminating as soon as we reach d – Need to remember edges traversed • Use depth – first search ? • Use breath – first search?
  • 31. DFS vs. BFS E F G B CD A start destination A DFS on A A DFS on BB A DFS on C B C A B Return to call on B D Call DFS on D A B D Call DFS on GG found destination - done! Path is implicitly stored in DFS recursion Path is: A, B, D, G DFS Process
  • 32. DFS vs. BFS E F G B CD A start destination BFS Process A Initial call to BFS on A Add A to queue B Dequeue A Add B frontrear frontrear C Dequeue B Add C, D frontrear D D Dequeue C Nothing to add frontrear G Dequeue D Add G frontrear found destination - done! Path must be stored separately