SlideShare a Scribd company logo
ECE 250 Algorithms and Data Structures
Douglas Wilhelm Harder, M.Math. LEL
Department of Electrical and Computer Engineering
University of Waterloo
Waterloo, Ontario, Canada
ece.uwaterloo.ca
dwharder@alumni.uwaterloo.ca
© 2006-2013 by Douglas Wilhelm Harder. Some rights reserved.
Graph traversals
2
Graph traversals
Outline
We will look at traversals of graphs
– Breadth-first or depth-first traversals
– Must avoid cycles
– Depth-first traversals can be recursive or iterative
– Problems that can be solved using traversals
3
Graph traversals
Strategies
Traversals of graphs are also called searches
We can use either breadth-first or depth-first traversals
– Breadth-first requires a queue
– Depth-first requires a stack
We each case, we will have to track which vertices have been
visited requiring Q(|V|) memory
– One option is a hash table
– If we can use a bit array, this requires only |V|/8 bytes
The time complexity cannot be better than and should not be worse
than Q(|V| + |E|)
– Connected graphs simplify this to Q(|E|)
– Worst case: Q(|V|2
)
4
Graph traversals
Breadth-first traversal
Consider implementing a breadth-first traversal on a graph:
– Choose any vertex, mark it as visited and push it onto queue
– While the queue is not empty:
• Pop to top vertex v from the queue
• For each vertex adjacent to v that has not been visited:
– Mark it visited, and
– Push it onto the queue
This continues until the queue is empty
– Note: if there are no unvisited vertices, the graph is connected,
5
Graph traversals
Iterative depth-first traversal
An implementation can use a queue
void Graph::depth_first_traversal( Vertex *first ) const {
unordered_map<Vertex *, int> hash;
hash.insert( first );
std::queue<Vertex *> queue;
queue.push( first );
while ( !queue.empty() ) {
Vertex *v = queue.front();
queue.pop();
// Perform an operation on v
for ( Vertex *w : v->adjacent_vertices() ) {
if ( !hash.member( w ) ) {
hash.insert( w );
queue.push( w );
}
}
}
}
6
Graph traversals
Breadth-first traversal
The size of the queue is O(|V|)
– The size depends both on:
• The number of edges, and
• The out-degree of the vertices
7
Graph traversals
Depth-first traversal
Consider implementing a depth-first traversal on a graph:
– Choose any vertex, mark it as visited
– From that vertex:
• If there is another adjacent vertex not yet visited, go to it
• Otherwise, go back to the most previous vertex that has not yet had all of its
adjacent vertices visited and continue from there
– Continue until no visited vertices have unvisited adjacent vertices
Two implementations:
– Recursive
– Iterative
8
Graph traversals
Recursive depth-first traversal
A recursive implementation uses the call stack for memory:
void Graph::depth_first_traversal( Vertex *first ) const {
std::unordered_map<Vertex *, int> hash;
hash.insert( first );
first->depth_first_traversal( hash );
}
void Vertex::depth_first_traversal( unordered_map<Vertex *, int> &hash )
const {
// Perform an operation on this
for ( Vertex *v : adjacent_vertices() ) {
if ( !hash.member( v ) ) {
hash.insert( v );
v->depth_first_traversal( hash );
}
}
}
9
Graph traversals
Iterative depth-first traversal
An iterative implementation can use a stack
void Graph::depth_first_traversal( Vertex *first ) const {
unordered_map<Vertex *, int> hash;
hash.insert( first );
std::stack<Vertex *> stack;
stack.push( first );
while ( !stack.empty() ) {
Vertex *v = stack.top();
stack.pop();
// Perform an operation on v
for ( Vertex *w : v->adjacent_vertices() ) {
if ( !hash.member( w ) ) {
hash.insert( w );
stack.push( w );
}
}
}
}
10
Graph traversals
Iterative depth-first traversal
If memory is an issue, we can reduce the stack size:
– For the vertex:
• Mark it as visited
• Perform an operation on that vertex
• Place it onto an empty stack
– While the stack is not empty:
• If the vertex on the top of the stack has an unvisited adjacent vertex,
– Mark it as visited
– Perform an operation on that vertex
– Place it onto the top of the stack
• Otherwise, pop the top of the stack
11
Graph traversals
Standard Template Library (STL) approach
An object-oriented STL approach would be create a iterator class:
– The hash table and stack/queue are private member variables
created in the constructor
– Internally, it would store the current node
– The auto-increment operator would pop the top of the stack and place
any unvisited adjacent vertices onto the stack/queue
– The auto-decrement operator would not be implemented
• You can’t go back…
12
Graph traversals
Example
Consider this graph
13
Graph traversals
Example
Performing a breadth-first traversal
– Push the first vertex onto the queue
A
14
Graph traversals
Example
Performing a breadth-first traversal
– Pop A and push B, C and E
A
B C E
15
Graph traversals
Example
Performing a breadth-first traversal:
– Pop B and push D
A, B
C E D
16
Graph traversals
Example
Performing a breadth-first traversal:
– Pop C and push F
A, B, C
E D F
17
Graph traversals
Example
Performing a breadth-first traversal:
– Pop E and push G and H
A, B, C, E
D F G H
18
Graph traversals
Example
Performing a breadth-first traversal:
– Pop D
A, B, C, E, D
F G H
19
Graph traversals
Example
Performing a breadth-first traversal:
– Pop F
A, B, C, E, D, F
G H
20
Graph traversals
Example
Performing a breadth-first traversal:
– Pop G and push I
A, B, C, E, D, F, G
H I
21
Graph traversals
Example
Performing a breadth-first traversal:
– Pop H
A, B, C, E, D, F, G, H
I
22
Graph traversals
Example
Performing a breadth-first traversal:
– Pop I
A, B, C, E, D, F, G, H, I
23
Graph traversals
Example
Performing a breadth-first traversal:
– The queue is empty: we are finished
A, B, C, E, D, F, G, H, I
24
Graph traversals
Example
Perform a recursive depth-first traversal on this same graph
25
Graph traversals
Example
Performing a recursive depth-first traversal:
– Visit the first node
A
26
Graph traversals
Example
Performing a recursive depth-first traversal:
– A has an unvisited neighbor
A, B
27
Graph traversals
Example
Performing a recursive depth-first traversal:
– B has an unvisited neighbor
A, B, C
28
Graph traversals
Example
Performing a recursive depth-first traversal:
– C has an unvisited neighbor
A, B, C, D
29
Graph traversals
Example
Performing a recursive depth-first traversal:
– D has no unvisited neighbors, so we return to C
A, B, C, D, E
30
Graph traversals
Example
Performing a recursive depth-first traversal:
– E has an unvisited neighbor
A, B, C, D, E, G
31
Graph traversals
Example
Performing a recursive depth-first traversal:
– F has an unvisited neighbor
A, B, C, D, E, G, I
32
Graph traversals
Example
Performing a recursive depth-first traversal:
– H has an unvisited neighbor
A, B, C, D, E, G, I, H
33
Graph traversals
Example
Performing a recursive depth-first traversal:
– We recurse back to C which has an unvisited neighbour
A, B, C, D, E, G, I, H, F
34
Graph traversals
Example
Performing a recursive depth-first traversal:
– We recurse finding that no other nodes have unvisited neighbours
A, B, C, D, E, G, I, H, F
35
Graph traversals
Comparison
Performing a recursive depth-first traversal:
– We recurse finding that no other nodes have unvisited neighbours
A, B, C, D, E, G, I, H, F
36
Graph traversals
The order in which vertices can differ greatly
– An iterative depth-first traversal may also be different again
Comparison
A, B, C, D, E, G, I, H, F
A, B, C, E, D, F, G, H, I
37
Graph traversals
Applications
Applications of tree traversals include:
– Determining connectiveness and finding connected sub-graphs
– Determining the path length from one vertex to all others
– Testing if a graph is bipartite
– Determining maximum flow
– Cheney’s algorithm for garbage collection
38
Graph traversals
Summary
This topic covered graph traversals
– Considered breadth-first and depth-first traversals
– Depth-first traversals can recursive or iterative
– More overhead than traversals of rooted trees
– Considered a STL approach to the design
– Considered an example with both implementations
– They are also called searches
39
Graph traversals
References
Wikipedia, http://en.wikipedia.org/wiki/Graph_traversal
http://en.wikipedia.org/wiki/Depth-first_search
http://en.wikipedia.org/wiki/Breadth-first_search
These slides are provided for the ECE 250 Algorithms and Data Structures course. The
material in it reflects Douglas W. Harder’s best judgment in light of the information available to
him at the time of preparation. Any reliance on these course slides by any party for any other
purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for
damages, if any, suffered by any party as a result of decisions made or actions based on these
course slides for any other purpose than that for which it was intended.

More Related Content

PDF
Lecture13
PPTX
Lec 15-graph Searching in data structure and lgorithm.pptx
PPTX
Unit ii-ppt
PDF
Lecture 16 - Dijkstra's Algorithm.pdf
PDF
data structure and algorithm Chapter Five.pdf very important
PPT
Graphs in Data Structure
PPTX
Graph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma
Lecture13
Lec 15-graph Searching in data structure and lgorithm.pptx
Unit ii-ppt
Lecture 16 - Dijkstra's Algorithm.pdf
data structure and algorithm Chapter Five.pdf very important
Graphs in Data Structure
Graph Analytics - From the Whiteboard to Your Toolbox - Sam Lerma

Similar to Graphtraversals Data Structures and its types. Different types of graphs are Linear and Non-linear.ppt (20)

PPT
lecture 17
PDF
Topological Sort
PDF
DM986-Week 9 -Part 1-Path Planning and Obstacle Avoidance.pdf
PPTX
Unit 9 graph
PPTX
Unit ix graph
PPTX
CPE-121-Discrete-Math_Graph-Theory-Introduction.pptx
DOCX
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
PDF
LEC 12-DSALGO-GRAPHS(final12).pdf
PPT
HEURISTIC SEARCH IN ARTIFICIAL INTELLEGENCE
PPT
What is A-Star (A*) Algorithm in Artificial astar.ppt
PPTX
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
PPTX
Data structure Graph PPT ( BFS & DFS ) NOTES
DOCX
IntroductionTopological sorting is a common operation performed .docx
PPTX
141222 graphulo ingraphblas
 
PPTX
141205 graphulo ingraphblas
PPTX
CPE-121-Discrete-Math_Graph-Theory..pptx
PPT
22-graphs1-dfs-bfs.ppt
PPTX
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH.pptx
lecture 17
Topological Sort
DM986-Week 9 -Part 1-Path Planning and Obstacle Avoidance.pdf
Unit 9 graph
Unit ix graph
CPE-121-Discrete-Math_Graph-Theory-Introduction.pptx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
LEC 12-DSALGO-GRAPHS(final12).pdf
HEURISTIC SEARCH IN ARTIFICIAL INTELLEGENCE
What is A-Star (A*) Algorithm in Artificial astar.ppt
WEB DEVELOPMET FRONT END WITH ADVANCED RECEAT
Data structure Graph PPT ( BFS & DFS ) NOTES
IntroductionTopological sorting is a common operation performed .docx
141222 graphulo ingraphblas
 
141205 graphulo ingraphblas
CPE-121-Discrete-Math_Graph-Theory..pptx
22-graphs1-dfs-bfs.ppt
UNIT IV NON LINEAR DATA STRUCTURES - GRAPH.pptx
Ad

Recently uploaded (20)

PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Current and future trends in Computer Vision.pptx
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
composite construction of structures.pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPT
introduction to datamining and warehousing
PPT
Project quality management in manufacturing
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Well-logging-methods_new................
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Current and future trends in Computer Vision.pptx
UNIT 4 Total Quality Management .pptx
bas. eng. economics group 4 presentation 1.pptx
Internet of Things (IOT) - A guide to understanding
Automation-in-Manufacturing-Chapter-Introduction.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
composite construction of structures.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
introduction to datamining and warehousing
Project quality management in manufacturing
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Well-logging-methods_new................
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Ad

Graphtraversals Data Structures and its types. Different types of graphs are Linear and Non-linear.ppt

  • 1. ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca dwharder@alumni.uwaterloo.ca © 2006-2013 by Douglas Wilhelm Harder. Some rights reserved. Graph traversals
  • 2. 2 Graph traversals Outline We will look at traversals of graphs – Breadth-first or depth-first traversals – Must avoid cycles – Depth-first traversals can be recursive or iterative – Problems that can be solved using traversals
  • 3. 3 Graph traversals Strategies Traversals of graphs are also called searches We can use either breadth-first or depth-first traversals – Breadth-first requires a queue – Depth-first requires a stack We each case, we will have to track which vertices have been visited requiring Q(|V|) memory – One option is a hash table – If we can use a bit array, this requires only |V|/8 bytes The time complexity cannot be better than and should not be worse than Q(|V| + |E|) – Connected graphs simplify this to Q(|E|) – Worst case: Q(|V|2 )
  • 4. 4 Graph traversals Breadth-first traversal Consider implementing a breadth-first traversal on a graph: – Choose any vertex, mark it as visited and push it onto queue – While the queue is not empty: • Pop to top vertex v from the queue • For each vertex adjacent to v that has not been visited: – Mark it visited, and – Push it onto the queue This continues until the queue is empty – Note: if there are no unvisited vertices, the graph is connected,
  • 5. 5 Graph traversals Iterative depth-first traversal An implementation can use a queue void Graph::depth_first_traversal( Vertex *first ) const { unordered_map<Vertex *, int> hash; hash.insert( first ); std::queue<Vertex *> queue; queue.push( first ); while ( !queue.empty() ) { Vertex *v = queue.front(); queue.pop(); // Perform an operation on v for ( Vertex *w : v->adjacent_vertices() ) { if ( !hash.member( w ) ) { hash.insert( w ); queue.push( w ); } } } }
  • 6. 6 Graph traversals Breadth-first traversal The size of the queue is O(|V|) – The size depends both on: • The number of edges, and • The out-degree of the vertices
  • 7. 7 Graph traversals Depth-first traversal Consider implementing a depth-first traversal on a graph: – Choose any vertex, mark it as visited – From that vertex: • If there is another adjacent vertex not yet visited, go to it • Otherwise, go back to the most previous vertex that has not yet had all of its adjacent vertices visited and continue from there – Continue until no visited vertices have unvisited adjacent vertices Two implementations: – Recursive – Iterative
  • 8. 8 Graph traversals Recursive depth-first traversal A recursive implementation uses the call stack for memory: void Graph::depth_first_traversal( Vertex *first ) const { std::unordered_map<Vertex *, int> hash; hash.insert( first ); first->depth_first_traversal( hash ); } void Vertex::depth_first_traversal( unordered_map<Vertex *, int> &hash ) const { // Perform an operation on this for ( Vertex *v : adjacent_vertices() ) { if ( !hash.member( v ) ) { hash.insert( v ); v->depth_first_traversal( hash ); } } }
  • 9. 9 Graph traversals Iterative depth-first traversal An iterative implementation can use a stack void Graph::depth_first_traversal( Vertex *first ) const { unordered_map<Vertex *, int> hash; hash.insert( first ); std::stack<Vertex *> stack; stack.push( first ); while ( !stack.empty() ) { Vertex *v = stack.top(); stack.pop(); // Perform an operation on v for ( Vertex *w : v->adjacent_vertices() ) { if ( !hash.member( w ) ) { hash.insert( w ); stack.push( w ); } } } }
  • 10. 10 Graph traversals Iterative depth-first traversal If memory is an issue, we can reduce the stack size: – For the vertex: • Mark it as visited • Perform an operation on that vertex • Place it onto an empty stack – While the stack is not empty: • If the vertex on the top of the stack has an unvisited adjacent vertex, – Mark it as visited – Perform an operation on that vertex – Place it onto the top of the stack • Otherwise, pop the top of the stack
  • 11. 11 Graph traversals Standard Template Library (STL) approach An object-oriented STL approach would be create a iterator class: – The hash table and stack/queue are private member variables created in the constructor – Internally, it would store the current node – The auto-increment operator would pop the top of the stack and place any unvisited adjacent vertices onto the stack/queue – The auto-decrement operator would not be implemented • You can’t go back…
  • 13. 13 Graph traversals Example Performing a breadth-first traversal – Push the first vertex onto the queue A
  • 14. 14 Graph traversals Example Performing a breadth-first traversal – Pop A and push B, C and E A B C E
  • 15. 15 Graph traversals Example Performing a breadth-first traversal: – Pop B and push D A, B C E D
  • 16. 16 Graph traversals Example Performing a breadth-first traversal: – Pop C and push F A, B, C E D F
  • 17. 17 Graph traversals Example Performing a breadth-first traversal: – Pop E and push G and H A, B, C, E D F G H
  • 18. 18 Graph traversals Example Performing a breadth-first traversal: – Pop D A, B, C, E, D F G H
  • 19. 19 Graph traversals Example Performing a breadth-first traversal: – Pop F A, B, C, E, D, F G H
  • 20. 20 Graph traversals Example Performing a breadth-first traversal: – Pop G and push I A, B, C, E, D, F, G H I
  • 21. 21 Graph traversals Example Performing a breadth-first traversal: – Pop H A, B, C, E, D, F, G, H I
  • 22. 22 Graph traversals Example Performing a breadth-first traversal: – Pop I A, B, C, E, D, F, G, H, I
  • 23. 23 Graph traversals Example Performing a breadth-first traversal: – The queue is empty: we are finished A, B, C, E, D, F, G, H, I
  • 24. 24 Graph traversals Example Perform a recursive depth-first traversal on this same graph
  • 25. 25 Graph traversals Example Performing a recursive depth-first traversal: – Visit the first node A
  • 26. 26 Graph traversals Example Performing a recursive depth-first traversal: – A has an unvisited neighbor A, B
  • 27. 27 Graph traversals Example Performing a recursive depth-first traversal: – B has an unvisited neighbor A, B, C
  • 28. 28 Graph traversals Example Performing a recursive depth-first traversal: – C has an unvisited neighbor A, B, C, D
  • 29. 29 Graph traversals Example Performing a recursive depth-first traversal: – D has no unvisited neighbors, so we return to C A, B, C, D, E
  • 30. 30 Graph traversals Example Performing a recursive depth-first traversal: – E has an unvisited neighbor A, B, C, D, E, G
  • 31. 31 Graph traversals Example Performing a recursive depth-first traversal: – F has an unvisited neighbor A, B, C, D, E, G, I
  • 32. 32 Graph traversals Example Performing a recursive depth-first traversal: – H has an unvisited neighbor A, B, C, D, E, G, I, H
  • 33. 33 Graph traversals Example Performing a recursive depth-first traversal: – We recurse back to C which has an unvisited neighbour A, B, C, D, E, G, I, H, F
  • 34. 34 Graph traversals Example Performing a recursive depth-first traversal: – We recurse finding that no other nodes have unvisited neighbours A, B, C, D, E, G, I, H, F
  • 35. 35 Graph traversals Comparison Performing a recursive depth-first traversal: – We recurse finding that no other nodes have unvisited neighbours A, B, C, D, E, G, I, H, F
  • 36. 36 Graph traversals The order in which vertices can differ greatly – An iterative depth-first traversal may also be different again Comparison A, B, C, D, E, G, I, H, F A, B, C, E, D, F, G, H, I
  • 37. 37 Graph traversals Applications Applications of tree traversals include: – Determining connectiveness and finding connected sub-graphs – Determining the path length from one vertex to all others – Testing if a graph is bipartite – Determining maximum flow – Cheney’s algorithm for garbage collection
  • 38. 38 Graph traversals Summary This topic covered graph traversals – Considered breadth-first and depth-first traversals – Depth-first traversals can recursive or iterative – More overhead than traversals of rooted trees – Considered a STL approach to the design – Considered an example with both implementations – They are also called searches
  • 39. 39 Graph traversals References Wikipedia, http://en.wikipedia.org/wiki/Graph_traversal http://en.wikipedia.org/wiki/Depth-first_search http://en.wikipedia.org/wiki/Breadth-first_search These slides are provided for the ECE 250 Algorithms and Data Structures course. The material in it reflects Douglas W. Harder’s best judgment in light of the information available to him at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.