SlideShare a Scribd company logo
Graph
Terminologies
V = {a,b,c,d,e,f}
E = {{a,b},{a,c},{b,d},
{c,d},{b,e},{c,f},
{e,f}}
• V1 is adjacent to v2 if and only if {v1,v2} ϵ E
• V1 is incident on e1 if an edge, e1, is connected
to a vertex, v1
Representation of Graph
• Adjacency matrix
– Use 2-dimensional matrix
• Adjacency list
– Use 1-dimensional array of Linked-list
Adjacency Matrix
• 2D array A[0..n-1, 0..n-1], where n is the number of vertices in the
graph
• Each row and column is indexed by the vertex id.
e,g a=0, b=1, c=2, d=3, e=4
• An array entry A[i][j]=1 if there is an edge connecting vertices i and j.
Otherwise, A[i][j]=0.
• The storage requirement is ϴ(n2).
• Not efficient if the graph has few edges.
• We can detect in O(1) time whether two vertices are connected.
Adjacency List
• is an array A[0..n-1] of lists, where n is the number of
vertices in the graph.
• Each array entry is indexed by the vertex id (as with adjacency matrix)
• The list A[i] stores the ids of the vertices adjacent to i.
Graph / Slide 6
Adjacency Matrix
2
4
3
5
1
7
6
9
8
0
0 1 2 3 4 5 6 7 8 9
0 0 0 0 0 0 0 0 0 1 0
1 0 0 1 1 0 0 0 1 0 1
2 0 1 0 0 1 0 0 0 1 0
3 0 1 0 0 1 1 0 0 0 0
4 0 0 1 1 0 0 0 0 0 0
5 0 0 0 1 0 0 1 0 0 0
6 0 0 0 0 0 1 0 1 0 0
7 0 1 0 0 0 0 1 0 0 0
8 1 0 1 0 0 0 0 0 0 1
9 0 1 0 0 0 0 0 0 1 0
Graph / Slide 7
Adjacency List
2
4
3
5
1
7
6
9
8
0
Adjacency Matrix vs. Adjacency List
Adjacency Matrix
• Always require n2 space
• waste a lot of space if
the number of edges
are sparse
• Can quickly find if an
edge exists
Adjacency List
• More compact than
adjacency matrices if
graph has few edges
• Requires more time to
find if an edge exists
Path
• A path is a sequence of vertices
(v0,v1,v2,…,vk) such that {vk,vk+1} ϵ E
for 0 ≤ i < k.
• The length of path is the number of edges on
the path
Types of Path
• Simple path:
– if and only if it does not contain a vertex more
than once
• Cycle:
– if and only if v0= vk
– Beginning and end are the same vertex
A path contains a cycle if some vertex appears twice or
more
Graph traversal
1. Breadth First Search (BFS)
• Finding the shortest path
• …
2. Depth First Search (DFS)
• Topological sort
• Find cycle
• …
Breadth First Search (BFS)
Graph / Slide 13
BFS + Path Finding
initialize all pred[v] to -1
Record where you
came from
Graph / Slide 14
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited
Table
(T/F)
F
F
F
F
F
F
F
F
F
F
Q = { }
Initialize visited
table (all False)
Initialize Pred to -1
Initialize Q to be empty
-1
1
-1
-1
-1
-1
-1
-1
-1
-1
Pred
Graph / Slide 15
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
T
F
F
F
F
F
F
F
Q = { 2 }
Flag that 2 has
been visited.
Place source 2 on the queue.
-
-
-
-
-
-
-
-
-
-
Pred
Graph / Slide 16
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
T
T
F
T
F
F
F
T
F
Q = {2} → { 8, 1, 4 }
Mark neighbors
as visited.
Record in Pred
that we came
from 2.
Dequeue 2.
Place all unvisited neighbors of 2 on the queue
Neighbors
-
2
-
-
2
-
-
-
2
-
Pred
Graph / Slide 17
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
F
T
F
F
F
T
T
Q = { 8, 1, 4 } → { 1, 4, 0, 9 }
Mark new visited
Neighbors.
Record in Pred
that we came
from 8.
Dequeue 8.
-- Place all unvisited neighbors of 8 on the queue.
-- Notice that 2 is not placed on the queue again, it has been visited!
Neighbors
8
2
-
-
2
-
-
-
2
8
Pred
Graph / Slide 18
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 1, 4, 0, 9 } → { 4, 0, 9, 3, 7 }
Mark new visited
Neighbors.
Record in Pred
that we came
from 1.
Dequeue 1.
-- Place all unvisited neighbors of 1 on the queue.
-- Only nodes 3 and 7 haven’t been visited yet.
Neighbors
8
2
-
1
2
-
-
1
2
8
Pred
Graph / Slide 19
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 4, 0, 9, 3, 7 } → { 0, 9, 3, 7 }
Dequeue 4.
-- 4 has no unvisited neighbors!
Neighbors
8
2
-
1
2
-
-
1
2
8
Pred
Graph / Slide 20
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 0, 9, 3, 7 } → { 9, 3, 7 }
Dequeue 0.
-- 0 has no unvisited neighbors!
Neighbors
8
2
-
1
2
-
-
1
2
8
Pred
Graph / Slide 21
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
T
T
T
Q = { 9, 3, 7 } → { 3, 7 }
Dequeue 9.
-- 9 has no unvisited neighbors!
Neighbors
8
2
-
1
2
-
-
1
2
8
Pred
Graph / Slide 22
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
F
T
T
T
Q = { 3, 7 } → { 7, 5 }
Dequeue 3.
-- place neighbor 5 on the queue.
Neighbors
Mark new visited
Vertex 5.
Record in Pred
that we came
from 3.
8
2
-
1
2
3
-
1
2
8
Pred
Graph / Slide 23
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { 7, 5 } → { 5, 6 }
Dequeue 7.
-- place neighbor 6 on the queue.
Neighbors
Mark new visited
Vertex 6.
Record in Pred
that we came
from 7.
8
2
-
1
2
3
7
1
2
8
Pred
Graph / Slide 24
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { 5, 6} → { 6 }
Dequeue 5.
-- no unvisited neighbors of 5.
Neighbors
8
2
-
1
2
3
7
1
2
8
Pred
Graph / Slide 25
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { 6 } → { }
Dequeue 6.
-- no unvisited neighbors of 6.
Neighbors
8
2
-
1
2
3
7
1
2
8
Pred
Graph / Slide 26
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
Q = { } STOP!!! Q is empty!!!
Pred now can be traced backward
to report the path!
8
2
-
1
2
3
7
1
2
8
Pred
Graph / Slide 27
Path reporting
8
2
-
1
2
3
7
1
2
8
0
1
2
3
4
5
6
7
8
9
nodes visited from
Try some examples, report path from s to w:
myNode2.Path(0) =
myNode2.Path(6) =
myNode2.Path(1) =
The path returned is the shortest from s to w
(minimum number of edges).
w pred[w]
Graph / Slide 28
BFS tree
 The paths found by BFS is often drawn as a rooted tree (called
BFS tree), with the starting vertex as the root of the tree.
BFS tree for vertex s=2.
Question: What would a “level” order traversal tell you?
8
2
-
1
2
3
7
1
2
8
0
1
2
3
4
5
6
7
8
9
nodes visited from
w pred[w]
Graph / Slide 29
DFS Algorithm
Flag all vertices as not
visited
Flag yourself as visited
For unvisited neighbors,
call RDFS(w) recursively
We can also record the paths using pred[ ].
Graph / Slide 30
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
F
F
F
F
F
F
F
F
Initialize visited
table (all False)
Initialize Pred to -1
-
-
-
-
-
-
-
-
-
-
Pred
Graph / Slide 31
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
T
F
F
F
F
F
F
F
Mark 2 as visited
-
-
-
-
-
-
-
-
-
-
Pred
RDFS( 2 )
Now visit RDFS(8)
Graph / Slide 32
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
F
F
T
F
F
F
F
F
T
F
Mark 8 as visited
mark Pred[8]
-
-
-
-
-
-
-
-
2
-
Pred
RDFS( 2 )
RDFS(8)
2 is already visited, so visit RDFS(0)
Recursive
calls
Graph / Slide 33
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
F
T
F
F
F
F
F
T
F
Mark 0 as visited
Mark Pred[0]
8
-
-
-
-
-
-
-
2
-
Pred
RDFS( 2 )
RDFS(8)
RDFS(0) -> no unvisited neighbors, return
to call RDFS(8)
Recursive
calls
Graph / Slide 34
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
F
T
F
F
F
F
F
T
F
8
-
-
-
-
-
-
-
2
-
Pred
RDFS( 2 )
RDFS(8)
Now visit 9 -> RDFS(9)
Recursive
calls
Back to 8
Graph / Slide 35
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
F
T
F
F
F
F
F
T
T
Mark 9 as visited
Mark Pred[9]
8
-
-
-
-
-
-
-
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
-> visit 1, RDFS(1)
Recursive
calls
Graph / Slide 36
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
F
F
F
F
F
T
T
Mark 1 as visited
Mark Pred[1]
8
9
-
-
-
-
-
-
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
visit RDFS(3)
Recursive
calls
Graph / Slide 37
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
F
F
F
F
T
T
Mark 3 as visited
Mark Pred[3]
8
9
-
1
-
-
-
-
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
visit RDFS(4)
Recursive
calls
Graph / Slide 38
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
RDFS(4)  STOP all of 4’s neighbors have been visited
return back to call RDFS(3)
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
F
T
T
Mark 4 as visited
Mark Pred[4]
8
9
-
1
3
-
-
-
2
8
Pred
Recursive
calls
Graph / Slide 39
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
F
F
F
T
T
8
9
-
1
3
-
-
-
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
visit 5 -> RDFS(5)
Recursive
calls
Back to 3
Graph / Slide 40
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
F
F
T
T
8
9
-
1
3
3
-
-
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
RDFS(5)
3 is already visited, so visit 6 -> RDFS(6)
Recursive
calls
Mark 5 as visited
Mark Pred[5]
Graph / Slide 41
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
F
T
T
8
9
-
1
3
3
5
-
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
RDFS(5)
RDFS(6)
visit 7 -> RDFS(7)
Recursive
calls
Mark 6 as visited
Mark Pred[6]
Graph / Slide 42
Example
2
4
3
5
1
7
6
9
8
0
Adjacency List
source
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
RDFS(5)
RDFS(6)
RDFS(7) -> Stop no more unvisited neighbors
Recursive
calls
Mark 7 as visited
Mark Pred[7]
Graph / Slide 43
Example
Adjacency List
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
RDFS(5)
RDFS(6) -> Stop
Recursive
calls
2
4
3
5
1
7
6
9
8
0
source
Graph / Slide 44
Example
Adjacency List
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3)
RDFS(5) -> Stop
Recursive
calls
2
4
3
5
1
7
6
9
8
0
source
Graph / Slide 45
Example
Adjacency List
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1)
RDFS(3) -> Stop
Recursive
calls
2
4
3
5
1
7
6
9
8
0
source
Graph / Slide 46
Example
Adjacency List
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9)
RDFS(1) -> Stop
Recursive
calls
2
4
3
5
1
7
6
9
8
0
source
Graph / Slide 47
Example
Adjacency List
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
Pred
RDFS( 2 )
RDFS(8)
RDFS(9) -> StopRecursive
calls
2
4
3
5
1
7
6
9
8
0
source
Graph / Slide 48
Example
Adjacency List
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
Pred
RDFS( 2 )
RDFS(8) -> Stop
Recursive
calls
2
4
3
5
1
7
6
9
8
0
source
Graph / Slide 49
Example
Adjacency List
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
Pred
RDFS( 2 ) -> Stop
Recursive
calls
2
4
3
5
1
7
6
9
8
0
source
Graph / Slide 50
Example
Adjacency List
0
1
2
3
4
5
6
7
8
9
Visited Table (T/F)
T
T
T
T
T
T
T
T
T
T
8
9
-
1
3
3
5
6
2
8
Pred
Try some examples.
Path(0) ->
Path(6) ->
Path(7) ->
Check our paths, does DFS find valid paths? Yes.
2
4
3
5
1
7
6
9
8
0
source
Graph / Slide 51
Time Complexity of DFS
(Using adjacency list)
 We never visited a vertex more than once
 We had to examine all edges of the vertices
 We know Σvertex v degree(v) = 2m where m is the number of
edges
 So, the running time of DFS is proportional to the
number of edges and number of vertices (same as BFS)
 O(n + m)
 You will also see this written as:
 O(|v|+|e|) |v| = number of vertices (n)
|e| = number of edges (m)
Graph / Slide 52
DFS Tree
Resulting DFS-tree.
Notice it is much “deeper”
than the BFS tree.
Captures the structure of the recursive calls
- when we visit a neighbor w of v, we add w as
child of v
- whenever DFS returns from a vertex v, we
climb up in the tree from v to its parent
Spanning tree
unweighted Graph
• Through Breadth-First-Search & Depth-First-
Search
Spanning tree
unweighted Graph
Weighted graph
https://dzone.com/articles/algorithm-week-dijkstra
Spanning tree
weighted Graph
Spanning tree
weighted Graph
Kruskal’s algorithm
T={}
Kruskal’s algorithm
T={(a, d)}
Kruskal’s algorithm
T={(a, d), (d, c)}
Kruskal’s algorithm
T={(a, d), (d, c)}
Kruskal’s algorithm
T={(a, d), (d, c), (d, e)}
Kruskal’s algorithm
T={(a, d), (d, c), (d, e), (d, b)}
Kruskal’s algorithm
T={(a, d), (d, c), (d, e), (d, b)}
=7 + 9 + 23 + 32 = 71
Spanning tree
weighted Graph

More Related Content

PPTX
Quadratic Functions graph
PPT
Shunting yard
PPTX
Shunting yard algo
PPTX
5.stack
PDF
Lisp tutorial
PDF
4th Semeste Electronics and Communication Engineering (Dec-2015; Jan-2016) Qu...
PPTX
DOAG: Visual SQL Tuning
PPT
Bfs and dfs in data structure
Quadratic Functions graph
Shunting yard
Shunting yard algo
5.stack
Lisp tutorial
4th Semeste Electronics and Communication Engineering (Dec-2015; Jan-2016) Qu...
DOAG: Visual SQL Tuning
Bfs and dfs in data structure

What's hot (19)

PDF
5th Semeste Electronics and Communication Engineering (Dec-2015; Jan-2016) Qu...
PDF
Spm trial-2012-addmath-qa-kedah
PDF
6th Semeste Electronics and Communication Engineering (Dec-2015; Jan-2016) Qu...
PPT
Graphs bfs dfs
PPTX
Ip 5 discrete mathematics
PPT
PDF
Ma2211 TRANSFORMS AND PARTIAL DIFFERENTIAL EQUATIONS
PDF
PPTX
Discrete Math IP4 - Automata Theory
PPT
basic concepts of Functions
PPTX
130210107039 2130702
PDF
MATHS SYMBOLS - #3 - LOGARITHMS - DEFINITION - EXAMPLES
PPT
Data structure lecture7
PDF
7th Semester Electronics and Communication Engineering (Dec-2015; Jan-2016) Q...
PDF
Logarithms
PPT
1603 plane sections of real and complex tori
DOCX
Modul kertas 1
5th Semeste Electronics and Communication Engineering (Dec-2015; Jan-2016) Qu...
Spm trial-2012-addmath-qa-kedah
6th Semeste Electronics and Communication Engineering (Dec-2015; Jan-2016) Qu...
Graphs bfs dfs
Ip 5 discrete mathematics
Ma2211 TRANSFORMS AND PARTIAL DIFFERENTIAL EQUATIONS
Discrete Math IP4 - Automata Theory
basic concepts of Functions
130210107039 2130702
MATHS SYMBOLS - #3 - LOGARITHMS - DEFINITION - EXAMPLES
Data structure lecture7
7th Semester Electronics and Communication Engineering (Dec-2015; Jan-2016) Q...
Logarithms
1603 plane sections of real and complex tori
Modul kertas 1
Ad

Similar to Week12 graph (20)

PPT
PPTX
PPT
Breadth first search
PPTX
topologicalsort-using c++ as development language.pptx
PPTX
Topological sort
PPT
graphGraphGraph data structure data structure2.ppt
PPT
graphGraphGraph data structure data structure2.ppt
PPTX
Data Structures and Agorithm: DS 21 Graph Theory.pptx
PDF
Ford Fulkerson wjgnejgbnhjbdreryjerhsrgjhegeujtgsruyw (1).pdf
PPT
Directed Acyclic Graph
PPTX
Chapter 2 laplace transform
PDF
Algorithm Design and Complexity - Course 12
PPTX
Data structure
PDF
Scalable Online Betweenness Centrality in Evolving Graphs
PDF
Algorithms of graph
PDF
CS-102 Data Structure lectures on Graphs
PDF
CS-102 Data Structure lectures on Graphs
PPTX
6. Graphs
PPT
Laplace transforms
Breadth first search
topologicalsort-using c++ as development language.pptx
Topological sort
graphGraphGraph data structure data structure2.ppt
graphGraphGraph data structure data structure2.ppt
Data Structures and Agorithm: DS 21 Graph Theory.pptx
Ford Fulkerson wjgnejgbnhjbdreryjerhsrgjhegeujtgsruyw (1).pdf
Directed Acyclic Graph
Chapter 2 laplace transform
Algorithm Design and Complexity - Course 12
Data structure
Scalable Online Betweenness Centrality in Evolving Graphs
Algorithms of graph
CS-102 Data Structure lectures on Graphs
CS-102 Data Structure lectures on Graphs
6. Graphs
Laplace transforms
Ad

More from IIUM (20)

PDF
How to use_000webhost
PDF
Chapter 2
PDF
Chapter 1
PDF
Kreydle internship-multimedia
PDF
03phpbldgblock
PDF
Chap2 practice key
PDF
Group p1
PDF
Tutorial import n auto pilot blogspot friendly seo
PDF
Visual sceneperception encycloperception-sage-oliva2009
PDF
03 the htm_lforms
PDF
Exercise on algo analysis answer
PDF
Redo midterm
PDF
Heaps
PDF
Report format
PDF
Edpuzzle guidelines
PDF
Final Exam Paper
PDF
Final Exam Paper
PDF
Group assignment 1 s21516
PDF
Avl tree-rotations
PDF
Vpn
How to use_000webhost
Chapter 2
Chapter 1
Kreydle internship-multimedia
03phpbldgblock
Chap2 practice key
Group p1
Tutorial import n auto pilot blogspot friendly seo
Visual sceneperception encycloperception-sage-oliva2009
03 the htm_lforms
Exercise on algo analysis answer
Redo midterm
Heaps
Report format
Edpuzzle guidelines
Final Exam Paper
Final Exam Paper
Group assignment 1 s21516
Avl tree-rotations
Vpn

Recently uploaded (20)

PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Complications of Minimal Access Surgery at WLH
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Institutional Correction lecture only . . .
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
RMMM.pdf make it easy to upload and study
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Business Ethics Teaching Materials for college
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Complications of Minimal Access Surgery at WLH
STATICS OF THE RIGID BODIES Hibbelers.pdf
Basic Mud Logging Guide for educational purpose
Final Presentation General Medicine 03-08-2024.pptx
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Supply Chain Operations Speaking Notes -ICLT Program
Institutional Correction lecture only . . .
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Microbial diseases, their pathogenesis and prophylaxis
RMMM.pdf make it easy to upload and study
Module 4: Burden of Disease Tutorial Slides S2 2025
Business Ethics Teaching Materials for college
Microbial disease of the cardiovascular and lymphatic systems
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Abdominal Access Techniques with Prof. Dr. R K Mishra
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx

Week12 graph

  • 2. Terminologies V = {a,b,c,d,e,f} E = {{a,b},{a,c},{b,d}, {c,d},{b,e},{c,f}, {e,f}} • V1 is adjacent to v2 if and only if {v1,v2} ϵ E • V1 is incident on e1 if an edge, e1, is connected to a vertex, v1
  • 3. Representation of Graph • Adjacency matrix – Use 2-dimensional matrix • Adjacency list – Use 1-dimensional array of Linked-list
  • 4. Adjacency Matrix • 2D array A[0..n-1, 0..n-1], where n is the number of vertices in the graph • Each row and column is indexed by the vertex id. e,g a=0, b=1, c=2, d=3, e=4 • An array entry A[i][j]=1 if there is an edge connecting vertices i and j. Otherwise, A[i][j]=0. • The storage requirement is ϴ(n2). • Not efficient if the graph has few edges. • We can detect in O(1) time whether two vertices are connected.
  • 5. Adjacency List • is an array A[0..n-1] of lists, where n is the number of vertices in the graph. • Each array entry is indexed by the vertex id (as with adjacency matrix) • The list A[i] stores the ids of the vertices adjacent to i.
  • 6. Graph / Slide 6 Adjacency Matrix 2 4 3 5 1 7 6 9 8 0 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 1 0 0 0 1 0 1 2 0 1 0 0 1 0 0 0 1 0 3 0 1 0 0 1 1 0 0 0 0 4 0 0 1 1 0 0 0 0 0 0 5 0 0 0 1 0 0 1 0 0 0 6 0 0 0 0 0 1 0 1 0 0 7 0 1 0 0 0 0 1 0 0 0 8 1 0 1 0 0 0 0 0 0 1 9 0 1 0 0 0 0 0 0 1 0
  • 7. Graph / Slide 7 Adjacency List 2 4 3 5 1 7 6 9 8 0
  • 8. Adjacency Matrix vs. Adjacency List Adjacency Matrix • Always require n2 space • waste a lot of space if the number of edges are sparse • Can quickly find if an edge exists Adjacency List • More compact than adjacency matrices if graph has few edges • Requires more time to find if an edge exists
  • 9. Path • A path is a sequence of vertices (v0,v1,v2,…,vk) such that {vk,vk+1} ϵ E for 0 ≤ i < k. • The length of path is the number of edges on the path
  • 10. Types of Path • Simple path: – if and only if it does not contain a vertex more than once • Cycle: – if and only if v0= vk – Beginning and end are the same vertex A path contains a cycle if some vertex appears twice or more
  • 11. Graph traversal 1. Breadth First Search (BFS) • Finding the shortest path • … 2. Depth First Search (DFS) • Topological sort • Find cycle • …
  • 13. Graph / Slide 13 BFS + Path Finding initialize all pred[v] to -1 Record where you came from
  • 14. Graph / Slide 14 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) F F F F F F F F F F Q = { } Initialize visited table (all False) Initialize Pred to -1 Initialize Q to be empty -1 1 -1 -1 -1 -1 -1 -1 -1 -1 Pred
  • 15. Graph / Slide 15 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) F F T F F F F F F F Q = { 2 } Flag that 2 has been visited. Place source 2 on the queue. - - - - - - - - - - Pred
  • 16. Graph / Slide 16 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) F T T F T F F F T F Q = {2} → { 8, 1, 4 } Mark neighbors as visited. Record in Pred that we came from 2. Dequeue 2. Place all unvisited neighbors of 2 on the queue Neighbors - 2 - - 2 - - - 2 - Pred
  • 17. Graph / Slide 17 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T F T F F F T T Q = { 8, 1, 4 } → { 1, 4, 0, 9 } Mark new visited Neighbors. Record in Pred that we came from 8. Dequeue 8. -- Place all unvisited neighbors of 8 on the queue. -- Notice that 2 is not placed on the queue again, it has been visited! Neighbors 8 2 - - 2 - - - 2 8 Pred
  • 18. Graph / Slide 18 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T F F T T T Q = { 1, 4, 0, 9 } → { 4, 0, 9, 3, 7 } Mark new visited Neighbors. Record in Pred that we came from 1. Dequeue 1. -- Place all unvisited neighbors of 1 on the queue. -- Only nodes 3 and 7 haven’t been visited yet. Neighbors 8 2 - 1 2 - - 1 2 8 Pred
  • 19. Graph / Slide 19 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T F F T T T Q = { 4, 0, 9, 3, 7 } → { 0, 9, 3, 7 } Dequeue 4. -- 4 has no unvisited neighbors! Neighbors 8 2 - 1 2 - - 1 2 8 Pred
  • 20. Graph / Slide 20 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T F F T T T Q = { 0, 9, 3, 7 } → { 9, 3, 7 } Dequeue 0. -- 0 has no unvisited neighbors! Neighbors 8 2 - 1 2 - - 1 2 8 Pred
  • 21. Graph / Slide 21 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T F F T T T Q = { 9, 3, 7 } → { 3, 7 } Dequeue 9. -- 9 has no unvisited neighbors! Neighbors 8 2 - 1 2 - - 1 2 8 Pred
  • 22. Graph / Slide 22 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T F T T T Q = { 3, 7 } → { 7, 5 } Dequeue 3. -- place neighbor 5 on the queue. Neighbors Mark new visited Vertex 5. Record in Pred that we came from 3. 8 2 - 1 2 3 - 1 2 8 Pred
  • 23. Graph / Slide 23 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T Q = { 7, 5 } → { 5, 6 } Dequeue 7. -- place neighbor 6 on the queue. Neighbors Mark new visited Vertex 6. Record in Pred that we came from 7. 8 2 - 1 2 3 7 1 2 8 Pred
  • 24. Graph / Slide 24 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T Q = { 5, 6} → { 6 } Dequeue 5. -- no unvisited neighbors of 5. Neighbors 8 2 - 1 2 3 7 1 2 8 Pred
  • 25. Graph / Slide 25 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T Q = { 6 } → { } Dequeue 6. -- no unvisited neighbors of 6. Neighbors 8 2 - 1 2 3 7 1 2 8 Pred
  • 26. Graph / Slide 26 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T Q = { } STOP!!! Q is empty!!! Pred now can be traced backward to report the path! 8 2 - 1 2 3 7 1 2 8 Pred
  • 27. Graph / Slide 27 Path reporting 8 2 - 1 2 3 7 1 2 8 0 1 2 3 4 5 6 7 8 9 nodes visited from Try some examples, report path from s to w: myNode2.Path(0) = myNode2.Path(6) = myNode2.Path(1) = The path returned is the shortest from s to w (minimum number of edges). w pred[w]
  • 28. Graph / Slide 28 BFS tree  The paths found by BFS is often drawn as a rooted tree (called BFS tree), with the starting vertex as the root of the tree. BFS tree for vertex s=2. Question: What would a “level” order traversal tell you? 8 2 - 1 2 3 7 1 2 8 0 1 2 3 4 5 6 7 8 9 nodes visited from w pred[w]
  • 29. Graph / Slide 29 DFS Algorithm Flag all vertices as not visited Flag yourself as visited For unvisited neighbors, call RDFS(w) recursively We can also record the paths using pred[ ].
  • 30. Graph / Slide 30 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) F F F F F F F F F F Initialize visited table (all False) Initialize Pred to -1 - - - - - - - - - - Pred
  • 31. Graph / Slide 31 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) F F T F F F F F F F Mark 2 as visited - - - - - - - - - - Pred RDFS( 2 ) Now visit RDFS(8)
  • 32. Graph / Slide 32 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) F F T F F F F F T F Mark 8 as visited mark Pred[8] - - - - - - - - 2 - Pred RDFS( 2 ) RDFS(8) 2 is already visited, so visit RDFS(0) Recursive calls
  • 33. Graph / Slide 33 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T F T F F F F F T F Mark 0 as visited Mark Pred[0] 8 - - - - - - - 2 - Pred RDFS( 2 ) RDFS(8) RDFS(0) -> no unvisited neighbors, return to call RDFS(8) Recursive calls
  • 34. Graph / Slide 34 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T F T F F F F F T F 8 - - - - - - - 2 - Pred RDFS( 2 ) RDFS(8) Now visit 9 -> RDFS(9) Recursive calls Back to 8
  • 35. Graph / Slide 35 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T F T F F F F F T T Mark 9 as visited Mark Pred[9] 8 - - - - - - - 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) -> visit 1, RDFS(1) Recursive calls
  • 36. Graph / Slide 36 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T F F F F F T T Mark 1 as visited Mark Pred[1] 8 9 - - - - - - 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) visit RDFS(3) Recursive calls
  • 37. Graph / Slide 37 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T F F F F T T Mark 3 as visited Mark Pred[3] 8 9 - 1 - - - - 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) visit RDFS(4) Recursive calls
  • 38. Graph / Slide 38 RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(4)  STOP all of 4’s neighbors have been visited return back to call RDFS(3) Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T F F F T T Mark 4 as visited Mark Pred[4] 8 9 - 1 3 - - - 2 8 Pred Recursive calls
  • 39. Graph / Slide 39 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T F F F T T 8 9 - 1 3 - - - 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) visit 5 -> RDFS(5) Recursive calls Back to 3
  • 40. Graph / Slide 40 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T F F T T 8 9 - 1 3 3 - - 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) 3 is already visited, so visit 6 -> RDFS(6) Recursive calls Mark 5 as visited Mark Pred[5]
  • 41. Graph / Slide 41 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T F T T 8 9 - 1 3 3 5 - 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) RDFS(6) visit 7 -> RDFS(7) Recursive calls Mark 6 as visited Mark Pred[6]
  • 42. Graph / Slide 42 Example 2 4 3 5 1 7 6 9 8 0 Adjacency List source 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 - 1 3 3 5 6 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) RDFS(6) RDFS(7) -> Stop no more unvisited neighbors Recursive calls Mark 7 as visited Mark Pred[7]
  • 43. Graph / Slide 43 Example Adjacency List 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 - 1 3 3 5 6 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) RDFS(6) -> Stop Recursive calls 2 4 3 5 1 7 6 9 8 0 source
  • 44. Graph / Slide 44 Example Adjacency List 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 - 1 3 3 5 6 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) RDFS(5) -> Stop Recursive calls 2 4 3 5 1 7 6 9 8 0 source
  • 45. Graph / Slide 45 Example Adjacency List 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 - 1 3 3 5 6 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) RDFS(3) -> Stop Recursive calls 2 4 3 5 1 7 6 9 8 0 source
  • 46. Graph / Slide 46 Example Adjacency List 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 - 1 3 3 5 6 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) RDFS(1) -> Stop Recursive calls 2 4 3 5 1 7 6 9 8 0 source
  • 47. Graph / Slide 47 Example Adjacency List 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 - 1 3 3 5 6 2 8 Pred RDFS( 2 ) RDFS(8) RDFS(9) -> StopRecursive calls 2 4 3 5 1 7 6 9 8 0 source
  • 48. Graph / Slide 48 Example Adjacency List 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 - 1 3 3 5 6 2 8 Pred RDFS( 2 ) RDFS(8) -> Stop Recursive calls 2 4 3 5 1 7 6 9 8 0 source
  • 49. Graph / Slide 49 Example Adjacency List 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 - 1 3 3 5 6 2 8 Pred RDFS( 2 ) -> Stop Recursive calls 2 4 3 5 1 7 6 9 8 0 source
  • 50. Graph / Slide 50 Example Adjacency List 0 1 2 3 4 5 6 7 8 9 Visited Table (T/F) T T T T T T T T T T 8 9 - 1 3 3 5 6 2 8 Pred Try some examples. Path(0) -> Path(6) -> Path(7) -> Check our paths, does DFS find valid paths? Yes. 2 4 3 5 1 7 6 9 8 0 source
  • 51. Graph / Slide 51 Time Complexity of DFS (Using adjacency list)  We never visited a vertex more than once  We had to examine all edges of the vertices  We know Σvertex v degree(v) = 2m where m is the number of edges  So, the running time of DFS is proportional to the number of edges and number of vertices (same as BFS)  O(n + m)  You will also see this written as:  O(|v|+|e|) |v| = number of vertices (n) |e| = number of edges (m)
  • 52. Graph / Slide 52 DFS Tree Resulting DFS-tree. Notice it is much “deeper” than the BFS tree. Captures the structure of the recursive calls - when we visit a neighbor w of v, we add w as child of v - whenever DFS returns from a vertex v, we climb up in the tree from v to its parent
  • 53. Spanning tree unweighted Graph • Through Breadth-First-Search & Depth-First- Search
  • 63. Kruskal’s algorithm T={(a, d), (d, c), (d, e), (d, b)}
  • 64. Kruskal’s algorithm T={(a, d), (d, c), (d, e), (d, b)} =7 + 9 + 23 + 32 = 71