SlideShare a Scribd company logo
1 of 60
Download to read offline
College of Computing & Information Technology
King Abdulaziz University
CPCS-204 – Data Structures I
Graphs Intro.
page 2
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Northwest Airline Flight
Boston
Hartford
Atlanta
Minneapolis
Austin
SF
Seattle
Anchorage
page 3
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Computer Network Or Internet
Comcast
Regional Network
Intel UNT
Charter
page 4
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Application
 Traveling Saleman
 Find the shortest path that connects all cities
without a loop.
Start
page 5
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Concepts of Graphs
edges (weight)
node or vertex
page 6
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Graph Definition
 A graph G = (V,E) is composed of:
V: set of vertices (nodes)
E: set of edges (arcs) 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)}
page 7
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Undirected vs. Directed Graph
Undirected Graph
– edge has no oriented
Directed Graph
– edge has oriented vertex
page 8
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Subgraph
 Subgraph:
 subset of vertices and edges
page 9
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Simple Path
 A simple path is a path such that all vertices are
distinct, except that the first and the last could
be the same.
 ABCD is a simple path
B
C
D
A
path
page 10
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Cycle
 A cycle is a path that starts and ends at the
same point. For undirected graph, the edges
are distinct.
 CBDC is a cycle
B
C
D
A
page 11
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Connected vs. Unconnected Graph
Connected Graph Unconnected Graph
page 12
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Directed Acyclic Graph
 Directed Acyclic Graph (DAG) : directed graph
without cycle
page 13
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Weighted Graph
 Weighted graph: a graph with numbers
assigned to its edges
 Weight: cost, distance, travel time, hop, etc.
0
1
3
2
20
10
1
5
4
page 14
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Representation Of Graph
 Two representations
 Adjacency Matrix
 Adjacency List
page 15
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Adjacency Matrix
 Assume N nodes in graph
 Use Matrix A[0…N-1][0…N-1]
 if vertex i and vertex j are adjacent in graph, A[i][j] =
1,
 otherwise A[i][j] = 0
 if vertex i has a loop, A[i][i] = 1
 if vertex i has no loop, A[i][i] = 0
page 16
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Example of Adjacency Matrix
0
1
3
2
A[i][j] 0 1 2 3
0 0 1 1 0
1 1 0 1 1
2 1 1 0 1
3 0 1 1 0
So, Matrix A =
0 1 1 0
1 0 1 1
1 1 0 1
0 1 1 0
page 17
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Undirected vs. Directed
 Undirected graph
 adjacency matrix is symmetric
 A[i][j]=A[j][i]
 Directed graph
 adjacency matrix may not be symmetric
 A[i][j]≠A[j][i]
page 18
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Directed Graph
A[i][j] 0 1 2 3
0 0 1 1 1
1 0 0 0 1
2 0 0 0 1
3 0 0 0 0
0
1
3
2
So, Matrix A =
0 1 1 1
0 0 0 1
0 0 0 1
0 0 0 0
page 19
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Weighted Graph
A[i][j] 0 1 2 3
0 0 20 10 1
1 20 0 0 5
2 10 0 0 4
3 1 5 4 0
0
1
3
2
10
20
1
4
5
So, Matrix A =
0 20 10 1
20 0 0 5
10 0 0 4
1 5 4 0
page 20
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Adjacency List
 An array of lists
 the ith element of the array is a list of vertices that
connect to vertex i
0
1
3
2
0
1
2
3
1 2 3
3
3
vertex 0 connect to vertex 1, 2 and 3
vertex 1 connects to 3
vertex 2 connects to 3
page 21
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Weighted Graph
 Weighted graph: extend each node with an
addition field: weight
0
1
3
2
20
10
1
5
4
0
1
2
3
1 10 2 20 3 1
0 10 3 4
0 20 3 5
0 1 1 4 2 5
page 22
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Comparison Of Representations
Cost
Adjacency
Matrix
Adjacency
List
Given two vertices u and v:
find out whether u and v are
adjacent
O(1)
degree of
node
O(N)
Given a vertex u:
enumerate all neighbors of u
O(N)
degree of
node
O(N)
For all vertices:
enumerate all neighbors of each
vertex
O(N2)
Summations
of all node
degree
O(E)
page 23
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Complete Graph
Total number of edges
in graph:
E = N(N-1)/2 = O(N2)
• There is an edge between any two vertices
page 24
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Sparse Graph
For example:
E = N-1= O(N)
• There is a very small number of edges in the
graph
page 25
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Brief Interlude: FAIL Picture
Pool Fail
‫ﻣﺳﺑﺢ‬
page 26
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Graph Traversal
 List out all cities that United Airline can reach
from Hartford Airport
CHI
LA
SF
NYC
Hartford
DC
page 27
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Graph Traversal
 From vertex u, list out all vertices that can be
reached in graph G
 Set of nodes to expand
 We basically have to go through all the nodes
 Each node has a “flag” that indicates if we have
visited it or not
page 28
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Traversal Algorithm
 Step 1: { Hartford }
 find neighbors of Hartford
 { Hartford, NYC, CHI }
CHI
NYC
LA
SF Hartford
W. DC
page 29
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Traversal Algorithm
 Step 2: { Hartford, NYC, CHI }
 find neighbors of NYC, CHI
 { Hartford, NYC, CHI, LA, SF }
CHI
NYC
LA
SF Hartford
W. DC
page 30
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Traversal Algorithm
 Step 3: {Hartford, NYC, CHI, LA, SF }
 find neighbors of LA, SF
 no other new neighbors
CHI
NYC
LA
SF Hartford
W. DC
page 31
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Traversal Algorithm
 Finally we get all cities that United Airline can
reach from Hartford Airport
 {Hartford, NYC, CHI, LA, SF }
CHI
NYC
LA
SF Hartford
W. DC
page 32
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Algorithm of Graph Traversal
1. Mark all nodes as unvisited
2. Pick a starting vertex u, add u to probing list
3. While ( probing list is not empty)
{
Remove a node v from probing list
Mark node v as visited
For each neighbor w of v, if w is unvisited,
add w to the probing list
}
page 33
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Graph Traversal Algorithms
 Two algorithms
 Depth First Traversal
 Breadth First Traversal
page 34
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Depth First Traversal
 Probing List is implemented as stack (LIFO)
 Example
 A’s neighbor: B, C, E
 B’s neighbor: A, C, F
 C’s neighbor: A, B, D
 D’s neighbor: E, C, F
 E’s neighbor: A, D
 F’s neighbor: B, D
 start from vertex A
A
B
C E
F D
page 35
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Depth First Traversal (Cont)
 Initial State
 Visited Vertices { }
 Probing Vertices { A }
 Unvisited Vertices { A, B, C, D, E, F }
A
B
C E
F D
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
A
stack
page 36
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Depth First Traversal (Cont)
 Pick a vertex from stack, it is A, mark
it as visited
 Find A’s first unvisited neighbor, push
it into stack
 Visited Vertices { A }
 Probing vertices { A, B }
 Unvisited Vertices { B, C, D, E, F }
A
B
C E
F D
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
B
A
stack
A
page 37
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Depth First Traversal (Cont)
 Pick a vertex from stack, it is B,
mark it as visited
 Find B’s first unvisited neighbor,
push it in stack
 Visited Vertices { A, B }
 Probing Vertices { A, B, C }
 Unvisited Vertices { C, D, E, F }
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
C
B
A
stack
B
A
A
B
C E
F D
page 38
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Depth First Traversal (Cont)
 Pick a vertex from stack, it is C, mark
it as visited
 Find C’s first unvisited neighbor, push
it in stack
 Visited Vertices { A, B, C }
 Probing Vertices { A, B, C, D }
 Unvisited Vertices { D, E, F }
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
stack
A
B
C E
F D
D
C
B
A
C
B
A
page 39
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Depth First Traversal (Cont)
 Pick a vertex from stack, it is D,
mark it as visited
 Find D’s first unvisited neighbor,
push it in stack
 Visited Vertices { A, B, C, D }
 Probing Vertices { A, B, C, D, E }
 Unvisited Vertices { E, F }
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
stack
A
B
C E
F D
D
C
B
E
A
D
C
B
A
page 40
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Depth First Traversal (Cont)
 Pick a vertex from stack, it is E, mark
it as visited
 Find E’s first unvisited neighbor, no
vertex found, Pop E
 Visited Vertices { A, B, C, D, E }
 Probing Vertices { A, B, C, D }
 Unvisited Vertices { F }
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
stack
A
B
C E
F D
D
C
B
A
D
C
B
E
A
page 41
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Depth First Traversal (Cont)
 Pick a vertex from stack, it is D, mark
it as visited
 Find D’s first unvisited neighbor,
push it in stack
 Visited Vertices { A, B, C, D, E }
 Probing Vertices { A, B, C, D, F}
 Unvisited Vertices { F }
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
stack
A
B
C E
F D
D
C
B
F
A
D
C
B
A
page 42
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Depth First Traversal (Cont)
 Pick a vertex from stack, it is F, mark
it as visited
 Find F’s first unvisited neighbor, no
vertex found, Pop F
 Visited Vertices { A, B, C, D, E, F }
 Probing Vertices { A, B, C, D}
 Unvisited Vertices { }
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
stack
A
B
C E
F D
D
C
B
A
D
C
B
F
A
page 43
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Depth First Traversal (Cont)
 Pick a vertex from stack, it is D, mark
it as visited
 Find D’s first unvisited neighbor, no
vertex found, Pop D
 Visited Vertices { A, B, C, D, E, F }
 Probing Vertices { A, B, C }
 Unvisited Vertices { }
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
stack
A
B
C E
F D
C
B
A
D
C
B
A
page 44
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Depth First Traversal (Cont)
 Pick a vertex from stack, it is C, mark
it as visited
 Find C’s first unvisited neighbor, no
vertex found, Pop C
 Visited Vertices { A, B, C, D, E, F }
 Probing Vertices { A, B }
 Unvisited Vertices { }
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
stack
A
B
C E
F D
B
A
C
B
A
page 45
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Depth First Traversal (Cont)
 Pick a vertex from stack, it is B, mark
it as visited
 Find B’s first unvisited neighbor, no
vertex found, Pop B
 Visited Vertices { A, B, C, D, E, F }
 Probing Vertices { A }
 Unvisited Vertices { }
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
stack
A
B
C E
F D
A
B
A
page 46
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Depth First Traversal (Cont)
 Pick a vertex from stack, it is A, mark
it as visited
 Find A’s first unvisited neighbor, no
vertex found, Pop A
 Visited Vertices { A, B, C, D, E, F }
 Probing Vertices { }
 Unvisited Vertices { }
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
stack
A
B
C E
F D
A
page 47
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Depth First Traversal (Cont)
 Now probing list is empty
 End of Depth First Traversal
 Visited Vertices { A, B, C, D, E, F
}
 Probing Vertices { }
 Unvisited Vertices { }
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
stack
A
B
C E
F D
page 48
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Breadth First Traversal
 Probing List is implemented as queue
(FIFO)
 Example
 A’s neighbor: B C E
 B’s neighbor: A C F
 C’s neighbor: A B D
 D’s neighbor: E C F
 E’s neighbor: A D
 F’s neighbor: B D
 start from vertex A
A
B
C E
F D
page 49
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Breadth First Traversal (Cont)
 Initial State
 Visited Vertices { }
 Probing Vertices { A }
 Unvisited Vertices { A, B, C, D, E, F }
A
B
C E
F D
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
A
queue
page 50
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Breadth First Traversal (Cont)
 Delete first vertex from queue, it is A,
mark it as visited
 Find A’s all unvisited neighbors, mark
them as visited, put them into queue
 Visited Vertices { A, B, C, E }
 Probing Vertices { B, C, E }
 Unvisited Vertices { D, F }
A
B
C E
F D
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
A
queue
B E
C
page 51
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Breadth First Traversal (Cont)
 Delete first vertex from queue, it is B,
mark it as visited
 Find B’s all unvisited neighbors, mark
them as visited, put them into queue
 Visited Vertices { A, B, C, E, F }
 Probing Vertices { C, E, F }
 Unvisited Vertices { D }
A
B
C E
F D
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
B E
C
queue
C F
E
page 52
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Breadth First Traversal (Cont)
 Delete first vertex from queue, it is C,
mark it as visited
 Find C’s all unvisited neighbors, mark
them as visited, put them into queue
 Visited Vertices { A, B, C, E, F, D }
 Probing Vertices { E, F, D }
 Unvisited Vertices { }
A
B
C E
F D
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
C F
E
queue
E D
F
page 53
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Breadth First Traversal (Cont)
 Delete first vertex from queue, it is E,
mark it as visited
 Find E’s all unvisited neighbors, no
vertex found
 Visited Vertices { A, B, C, E, F, D }
 Probing Vertices { F, D }
 Unvisited Vertices { }
A
B
C E
F D
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
E D
F
queue
F D
page 54
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Breadth First Traversal (Cont)
 Delete first vertex from queue, it is F,
mark it as visited
 Find F’s all unvisited neighbors, no
vertex found
 Visited Vertices { A, B, C, E, F, D }
 Probing Vertices { D }
 Unvisited Vertices { }
A
B
C E
F D
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
F D
queue
D
page 55
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Breadth First Traversal (Cont)
 Delete first vertex from queue, it is D,
mark it as visited
 Find D’s all unvisited neighbors, no
vertex found
 Visited Vertices { A, B, C, E, F, D }
 Probing Vertices { }
 Unvisited Vertices { }
A
B
C E
F D
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
D
queue
page 56
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Breadth First Traversal (Cont)
 Now the queue is empty
 End of Breadth First Traversal
 Visited Vertices { A, B, C, E, F, D }
 Probing Vertices { }
 Unvisited Vertices { }
A
B
C E
F D
– A’s neighbor: B C E
– B’s neighbor: A C F
– C’s neighbor: A B D
– D’s neighbor: E C F
– E’s neighbor: A D
– F’s neighbor: B D
queue
page 57
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Difference Between DFT & BFT
 Depth First Traversal (DFT)
 order of visited: A, B, C, D, E, F
 Breadth First Traversal (BFT)
 order of visited: A, B, C, E, F, D
A
B
C E
F D
page 58
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Graph Intro.
WASN’T
THAT
RAVISHING!
page 59
Sorting: Quick Sort
© Dr. Jonathan (Yahya) Cazalas
Daily Demotivator
College of Computing & Information Technology
King Abdulaziz University
CPCS-204 – Data Structures I
Graphs Intro.

More Related Content

What's hot

2.2 linear equations and 2.3 Slope
2.2 linear equations and 2.3 Slope2.2 linear equations and 2.3 Slope
2.2 linear equations and 2.3 SlopeJessica Garcia
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsYi-Lung Tsai
 
Applied Calculus: An introduction about Real Numbers and Real Line
Applied Calculus: An introduction about Real Numbers and Real LineApplied Calculus: An introduction about Real Numbers and Real Line
Applied Calculus: An introduction about Real Numbers and Real Linebaetulilm
 
Properties of Parabola
Properties of ParabolaProperties of Parabola
Properties of Parabolarey castro
 
Linear equations part i
Linear equations part iLinear equations part i
Linear equations part icoburgmaths
 
Computational geometry
Computational geometryComputational geometry
Computational geometrymurali9120
 
Analytic geom and distance formula
Analytic geom and distance formulaAnalytic geom and distance formula
Analytic geom and distance formulajustsayso
 
Analytic geometry lecture1
Analytic geometry lecture1Analytic geometry lecture1
Analytic geometry lecture1admercano101
 
Coordinate geometry
Coordinate geometryCoordinate geometry
Coordinate geometryCarl Davis
 

What's hot (20)

Maths project
Maths  projectMaths  project
Maths project
 
Analytic geometry basic concepts
Analytic geometry basic conceptsAnalytic geometry basic concepts
Analytic geometry basic concepts
 
Coordinate geometry
Coordinate geometryCoordinate geometry
Coordinate geometry
 
Coordinate geometry
Coordinate geometryCoordinate geometry
Coordinate geometry
 
2.2 linear equations and 2.3 Slope
2.2 linear equations and 2.3 Slope2.2 linear equations and 2.3 Slope
2.2 linear equations and 2.3 Slope
 
Problem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - GraphsProblem Solving with Algorithms and Data Structure - Graphs
Problem Solving with Algorithms and Data Structure - Graphs
 
Applied Calculus: An introduction about Real Numbers and Real Line
Applied Calculus: An introduction about Real Numbers and Real LineApplied Calculus: An introduction about Real Numbers and Real Line
Applied Calculus: An introduction about Real Numbers and Real Line
 
Properties of Parabola
Properties of ParabolaProperties of Parabola
Properties of Parabola
 
Linear equations part i
Linear equations part iLinear equations part i
Linear equations part i
 
Plano Numérico
Plano Numérico Plano Numérico
Plano Numérico
 
Maths presentation 22
Maths presentation 22Maths presentation 22
Maths presentation 22
 
Computational geometry
Computational geometryComputational geometry
Computational geometry
 
Chapter 1
Chapter 1 Chapter 1
Chapter 1
 
Data structure
Data structureData structure
Data structure
 
Math14 lesson 1
Math14 lesson 1Math14 lesson 1
Math14 lesson 1
 
Analytic geom and distance formula
Analytic geom and distance formulaAnalytic geom and distance formula
Analytic geom and distance formula
 
2.2 linear equations
2.2 linear equations2.2 linear equations
2.2 linear equations
 
C4 parametric curves_lesson
C4 parametric curves_lessonC4 parametric curves_lesson
C4 parametric curves_lesson
 
Analytic geometry lecture1
Analytic geometry lecture1Analytic geometry lecture1
Analytic geometry lecture1
 
Coordinate geometry
Coordinate geometryCoordinate geometry
Coordinate geometry
 

Similar to graphs (20)

Chapter 23 aoa
Chapter 23 aoaChapter 23 aoa
Chapter 23 aoa
 
Graph Introduction.ppt
Graph Introduction.pptGraph Introduction.ppt
Graph Introduction.ppt
 
lec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.pptlec 09-graphs-bfs-dfs.ppt
lec 09-graphs-bfs-dfs.ppt
 
graph.pptx
graph.pptxgraph.pptx
graph.pptx
 
Lecture 5b graphs and hashing
Lecture 5b graphs and hashingLecture 5b graphs and hashing
Lecture 5b graphs and hashing
 
Vectors and 3 d
Vectors and 3 dVectors and 3 d
Vectors and 3 d
 
2 depth first
2 depth first2 depth first
2 depth first
 
Graphs
GraphsGraphs
Graphs
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
 
Graps 2
Graps 2Graps 2
Graps 2
 
Graph
GraphGraph
Graph
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Graphs in data structures
Graphs in data structuresGraphs in data structures
Graphs in data structures
 
LEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdfLEC 12-DSALGO-GRAPHS(final12).pdf
LEC 12-DSALGO-GRAPHS(final12).pdf
 
Talk on Graph Theory - I
Talk on Graph Theory - ITalk on Graph Theory - I
Talk on Graph Theory - I
 
Graphs
GraphsGraphs
Graphs
 
Graphs
GraphsGraphs
Graphs
 
Directed Acyclic Graph
Directed Acyclic Graph Directed Acyclic Graph
Directed Acyclic Graph
 
Graph in Data Structure
Graph in Data StructureGraph in Data Structure
Graph in Data Structure
 
Graph 01
Graph 01Graph 01
Graph 01
 

More from Mohamed Elsayed (20)

binary_trees4
binary_trees4binary_trees4
binary_trees4
 
binary_trees3
binary_trees3binary_trees3
binary_trees3
 
binary_trees2
binary_trees2binary_trees2
binary_trees2
 
binary_trees1
binary_trees1binary_trees1
binary_trees1
 
queues
queuesqueues
queues
 
stacks2
stacks2stacks2
stacks2
 
stacks1
stacks1stacks1
stacks1
 
algorithm_analysis2
algorithm_analysis2algorithm_analysis2
algorithm_analysis2
 
algorithm_analysis1
algorithm_analysis1algorithm_analysis1
algorithm_analysis1
 
recursion3
recursion3recursion3
recursion3
 
recursion2
recursion2recursion2
recursion2
 
recursion1
recursion1recursion1
recursion1
 
linked_lists4
linked_lists4linked_lists4
linked_lists4
 
linked_lists3
linked_lists3linked_lists3
linked_lists3
 
Linked_lists2
Linked_lists2Linked_lists2
Linked_lists2
 
linked_lists
linked_listslinked_lists
linked_lists
 
sorted_listmatch
sorted_listmatchsorted_listmatch
sorted_listmatch
 
binary_search
binary_searchbinary_search
binary_search
 
arrays
arraysarrays
arrays
 
introduction
introductionintroduction
introduction
 

Recently uploaded

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Recently uploaded (20)

This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

graphs

  • 1. College of Computing & Information Technology King Abdulaziz University CPCS-204 – Data Structures I Graphs Intro.
  • 2. page 2 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Northwest Airline Flight Boston Hartford Atlanta Minneapolis Austin SF Seattle Anchorage
  • 3. page 3 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Computer Network Or Internet Comcast Regional Network Intel UNT Charter
  • 4. page 4 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Application  Traveling Saleman  Find the shortest path that connects all cities without a loop. Start
  • 5. page 5 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Concepts of Graphs edges (weight) node or vertex
  • 6. page 6 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Graph Definition  A graph G = (V,E) is composed of: V: set of vertices (nodes) E: set of edges (arcs) 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)}
  • 7. page 7 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Undirected vs. Directed Graph Undirected Graph – edge has no oriented Directed Graph – edge has oriented vertex
  • 8. page 8 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Subgraph  Subgraph:  subset of vertices and edges
  • 9. page 9 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Simple Path  A simple path is a path such that all vertices are distinct, except that the first and the last could be the same.  ABCD is a simple path B C D A path
  • 10. page 10 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Cycle  A cycle is a path that starts and ends at the same point. For undirected graph, the edges are distinct.  CBDC is a cycle B C D A
  • 11. page 11 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Connected vs. Unconnected Graph Connected Graph Unconnected Graph
  • 12. page 12 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Directed Acyclic Graph  Directed Acyclic Graph (DAG) : directed graph without cycle
  • 13. page 13 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Weighted Graph  Weighted graph: a graph with numbers assigned to its edges  Weight: cost, distance, travel time, hop, etc. 0 1 3 2 20 10 1 5 4
  • 14. page 14 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Representation Of Graph  Two representations  Adjacency Matrix  Adjacency List
  • 15. page 15 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Adjacency Matrix  Assume N nodes in graph  Use Matrix A[0…N-1][0…N-1]  if vertex i and vertex j are adjacent in graph, A[i][j] = 1,  otherwise A[i][j] = 0  if vertex i has a loop, A[i][i] = 1  if vertex i has no loop, A[i][i] = 0
  • 16. page 16 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Example of Adjacency Matrix 0 1 3 2 A[i][j] 0 1 2 3 0 0 1 1 0 1 1 0 1 1 2 1 1 0 1 3 0 1 1 0 So, Matrix A = 0 1 1 0 1 0 1 1 1 1 0 1 0 1 1 0
  • 17. page 17 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Undirected vs. Directed  Undirected graph  adjacency matrix is symmetric  A[i][j]=A[j][i]  Directed graph  adjacency matrix may not be symmetric  A[i][j]≠A[j][i]
  • 18. page 18 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Directed Graph A[i][j] 0 1 2 3 0 0 1 1 1 1 0 0 0 1 2 0 0 0 1 3 0 0 0 0 0 1 3 2 So, Matrix A = 0 1 1 1 0 0 0 1 0 0 0 1 0 0 0 0
  • 19. page 19 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Weighted Graph A[i][j] 0 1 2 3 0 0 20 10 1 1 20 0 0 5 2 10 0 0 4 3 1 5 4 0 0 1 3 2 10 20 1 4 5 So, Matrix A = 0 20 10 1 20 0 0 5 10 0 0 4 1 5 4 0
  • 20. page 20 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Adjacency List  An array of lists  the ith element of the array is a list of vertices that connect to vertex i 0 1 3 2 0 1 2 3 1 2 3 3 3 vertex 0 connect to vertex 1, 2 and 3 vertex 1 connects to 3 vertex 2 connects to 3
  • 21. page 21 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Weighted Graph  Weighted graph: extend each node with an addition field: weight 0 1 3 2 20 10 1 5 4 0 1 2 3 1 10 2 20 3 1 0 10 3 4 0 20 3 5 0 1 1 4 2 5
  • 22. page 22 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Comparison Of Representations Cost Adjacency Matrix Adjacency List Given two vertices u and v: find out whether u and v are adjacent O(1) degree of node O(N) Given a vertex u: enumerate all neighbors of u O(N) degree of node O(N) For all vertices: enumerate all neighbors of each vertex O(N2) Summations of all node degree O(E)
  • 23. page 23 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Complete Graph Total number of edges in graph: E = N(N-1)/2 = O(N2) • There is an edge between any two vertices
  • 24. page 24 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Sparse Graph For example: E = N-1= O(N) • There is a very small number of edges in the graph
  • 25. page 25 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Brief Interlude: FAIL Picture Pool Fail ‫ﻣﺳﺑﺢ‬
  • 26. page 26 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Graph Traversal  List out all cities that United Airline can reach from Hartford Airport CHI LA SF NYC Hartford DC
  • 27. page 27 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Graph Traversal  From vertex u, list out all vertices that can be reached in graph G  Set of nodes to expand  We basically have to go through all the nodes  Each node has a “flag” that indicates if we have visited it or not
  • 28. page 28 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Traversal Algorithm  Step 1: { Hartford }  find neighbors of Hartford  { Hartford, NYC, CHI } CHI NYC LA SF Hartford W. DC
  • 29. page 29 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Traversal Algorithm  Step 2: { Hartford, NYC, CHI }  find neighbors of NYC, CHI  { Hartford, NYC, CHI, LA, SF } CHI NYC LA SF Hartford W. DC
  • 30. page 30 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Traversal Algorithm  Step 3: {Hartford, NYC, CHI, LA, SF }  find neighbors of LA, SF  no other new neighbors CHI NYC LA SF Hartford W. DC
  • 31. page 31 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Traversal Algorithm  Finally we get all cities that United Airline can reach from Hartford Airport  {Hartford, NYC, CHI, LA, SF } CHI NYC LA SF Hartford W. DC
  • 32. page 32 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Algorithm of Graph Traversal 1. Mark all nodes as unvisited 2. Pick a starting vertex u, add u to probing list 3. While ( probing list is not empty) { Remove a node v from probing list Mark node v as visited For each neighbor w of v, if w is unvisited, add w to the probing list }
  • 33. page 33 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Graph Traversal Algorithms  Two algorithms  Depth First Traversal  Breadth First Traversal
  • 34. page 34 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Depth First Traversal  Probing List is implemented as stack (LIFO)  Example  A’s neighbor: B, C, E  B’s neighbor: A, C, F  C’s neighbor: A, B, D  D’s neighbor: E, C, F  E’s neighbor: A, D  F’s neighbor: B, D  start from vertex A A B C E F D
  • 35. page 35 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Depth First Traversal (Cont)  Initial State  Visited Vertices { }  Probing Vertices { A }  Unvisited Vertices { A, B, C, D, E, F } A B C E F D – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D A stack
  • 36. page 36 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Depth First Traversal (Cont)  Pick a vertex from stack, it is A, mark it as visited  Find A’s first unvisited neighbor, push it into stack  Visited Vertices { A }  Probing vertices { A, B }  Unvisited Vertices { B, C, D, E, F } A B C E F D – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D B A stack A
  • 37. page 37 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Depth First Traversal (Cont)  Pick a vertex from stack, it is B, mark it as visited  Find B’s first unvisited neighbor, push it in stack  Visited Vertices { A, B }  Probing Vertices { A, B, C }  Unvisited Vertices { C, D, E, F } – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D C B A stack B A A B C E F D
  • 38. page 38 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Depth First Traversal (Cont)  Pick a vertex from stack, it is C, mark it as visited  Find C’s first unvisited neighbor, push it in stack  Visited Vertices { A, B, C }  Probing Vertices { A, B, C, D }  Unvisited Vertices { D, E, F } – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D stack A B C E F D D C B A C B A
  • 39. page 39 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Depth First Traversal (Cont)  Pick a vertex from stack, it is D, mark it as visited  Find D’s first unvisited neighbor, push it in stack  Visited Vertices { A, B, C, D }  Probing Vertices { A, B, C, D, E }  Unvisited Vertices { E, F } – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D stack A B C E F D D C B E A D C B A
  • 40. page 40 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Depth First Traversal (Cont)  Pick a vertex from stack, it is E, mark it as visited  Find E’s first unvisited neighbor, no vertex found, Pop E  Visited Vertices { A, B, C, D, E }  Probing Vertices { A, B, C, D }  Unvisited Vertices { F } – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D stack A B C E F D D C B A D C B E A
  • 41. page 41 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Depth First Traversal (Cont)  Pick a vertex from stack, it is D, mark it as visited  Find D’s first unvisited neighbor, push it in stack  Visited Vertices { A, B, C, D, E }  Probing Vertices { A, B, C, D, F}  Unvisited Vertices { F } – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D stack A B C E F D D C B F A D C B A
  • 42. page 42 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Depth First Traversal (Cont)  Pick a vertex from stack, it is F, mark it as visited  Find F’s first unvisited neighbor, no vertex found, Pop F  Visited Vertices { A, B, C, D, E, F }  Probing Vertices { A, B, C, D}  Unvisited Vertices { } – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D stack A B C E F D D C B A D C B F A
  • 43. page 43 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Depth First Traversal (Cont)  Pick a vertex from stack, it is D, mark it as visited  Find D’s first unvisited neighbor, no vertex found, Pop D  Visited Vertices { A, B, C, D, E, F }  Probing Vertices { A, B, C }  Unvisited Vertices { } – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D stack A B C E F D C B A D C B A
  • 44. page 44 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Depth First Traversal (Cont)  Pick a vertex from stack, it is C, mark it as visited  Find C’s first unvisited neighbor, no vertex found, Pop C  Visited Vertices { A, B, C, D, E, F }  Probing Vertices { A, B }  Unvisited Vertices { } – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D stack A B C E F D B A C B A
  • 45. page 45 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Depth First Traversal (Cont)  Pick a vertex from stack, it is B, mark it as visited  Find B’s first unvisited neighbor, no vertex found, Pop B  Visited Vertices { A, B, C, D, E, F }  Probing Vertices { A }  Unvisited Vertices { } – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D stack A B C E F D A B A
  • 46. page 46 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Depth First Traversal (Cont)  Pick a vertex from stack, it is A, mark it as visited  Find A’s first unvisited neighbor, no vertex found, Pop A  Visited Vertices { A, B, C, D, E, F }  Probing Vertices { }  Unvisited Vertices { } – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D stack A B C E F D A
  • 47. page 47 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Depth First Traversal (Cont)  Now probing list is empty  End of Depth First Traversal  Visited Vertices { A, B, C, D, E, F }  Probing Vertices { }  Unvisited Vertices { } – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D stack A B C E F D
  • 48. page 48 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Breadth First Traversal  Probing List is implemented as queue (FIFO)  Example  A’s neighbor: B C E  B’s neighbor: A C F  C’s neighbor: A B D  D’s neighbor: E C F  E’s neighbor: A D  F’s neighbor: B D  start from vertex A A B C E F D
  • 49. page 49 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Breadth First Traversal (Cont)  Initial State  Visited Vertices { }  Probing Vertices { A }  Unvisited Vertices { A, B, C, D, E, F } A B C E F D – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D A queue
  • 50. page 50 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Breadth First Traversal (Cont)  Delete first vertex from queue, it is A, mark it as visited  Find A’s all unvisited neighbors, mark them as visited, put them into queue  Visited Vertices { A, B, C, E }  Probing Vertices { B, C, E }  Unvisited Vertices { D, F } A B C E F D – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D A queue B E C
  • 51. page 51 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Breadth First Traversal (Cont)  Delete first vertex from queue, it is B, mark it as visited  Find B’s all unvisited neighbors, mark them as visited, put them into queue  Visited Vertices { A, B, C, E, F }  Probing Vertices { C, E, F }  Unvisited Vertices { D } A B C E F D – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D B E C queue C F E
  • 52. page 52 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Breadth First Traversal (Cont)  Delete first vertex from queue, it is C, mark it as visited  Find C’s all unvisited neighbors, mark them as visited, put them into queue  Visited Vertices { A, B, C, E, F, D }  Probing Vertices { E, F, D }  Unvisited Vertices { } A B C E F D – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D C F E queue E D F
  • 53. page 53 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Breadth First Traversal (Cont)  Delete first vertex from queue, it is E, mark it as visited  Find E’s all unvisited neighbors, no vertex found  Visited Vertices { A, B, C, E, F, D }  Probing Vertices { F, D }  Unvisited Vertices { } A B C E F D – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D E D F queue F D
  • 54. page 54 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Breadth First Traversal (Cont)  Delete first vertex from queue, it is F, mark it as visited  Find F’s all unvisited neighbors, no vertex found  Visited Vertices { A, B, C, E, F, D }  Probing Vertices { D }  Unvisited Vertices { } A B C E F D – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D F D queue D
  • 55. page 55 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Breadth First Traversal (Cont)  Delete first vertex from queue, it is D, mark it as visited  Find D’s all unvisited neighbors, no vertex found  Visited Vertices { A, B, C, E, F, D }  Probing Vertices { }  Unvisited Vertices { } A B C E F D – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D D queue
  • 56. page 56 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Breadth First Traversal (Cont)  Now the queue is empty  End of Breadth First Traversal  Visited Vertices { A, B, C, E, F, D }  Probing Vertices { }  Unvisited Vertices { } A B C E F D – A’s neighbor: B C E – B’s neighbor: A C F – C’s neighbor: A B D – D’s neighbor: E C F – E’s neighbor: A D – F’s neighbor: B D queue
  • 57. page 57 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Difference Between DFT & BFT  Depth First Traversal (DFT)  order of visited: A, B, C, D, E, F  Breadth First Traversal (BFT)  order of visited: A, B, C, E, F, D A B C E F D
  • 58. page 58 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Graph Intro. WASN’T THAT RAVISHING!
  • 59. page 59 Sorting: Quick Sort © Dr. Jonathan (Yahya) Cazalas Daily Demotivator
  • 60. College of Computing & Information Technology King Abdulaziz University CPCS-204 – Data Structures I Graphs Intro.