SlideShare a Scribd company logo
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22
Graph Two standard ways to represent a graph Adjacency List Adjacency Matrix
Adjacency List Representation of a Graph For each  u  є   V , the adjacency list  Adj [ u ] contains all the vertices  v  such that there is an edge ( u ,  v )  є   E .
Adjacency Matrix Representation of a Graph The adjacency-matrix representation of a graph  G  consists of a | V | × | V | matrix  A  = ( aij ) such that a ij =1  if  (i,j)  є  E =o  otherwise
Copyright  © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright  © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Breath-first Search Breadth-first search  is one of the simplest algorithms for searching a graph Given a graph  G  = ( V ,  E ) and a distinguished  source  vertex  s , breadth-first search systematically explores the edges of  G  to "discover" every vertex that is reachable from  s .
Breath-first Search T he algorithm discovers all vertices at distance  k  from  s  before discovering any vertices at distance  k  + 1.
Breath-first Search To keep track of progress, breadth-first search colors each vertex white, gray, or black. All vertices start out white and may later become gray and then black.  A vertex is  discovered  the first time it is encountered during the search, at which time it becomes nonwhite.
Breath-first Search The color of each vertex  u is stored in the variable  color [ u ]. The predecessor of  u  is stored in the variable π[ u ].  The distance from the source  s  to vertex  u  computed by the algorithm is stored in  d [ u ]
Copyright  © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright  © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright  © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Depth-first Search The strategy followed by depth-first search is, as its name implies, to search "deeper" in the graph whenever possible.
Depth-first Search The predecessor sub-graph of a depth-first search forms a  depth-first forest  composed of several  depth-first trees . The edges in  Eπ  are called  tree edges .
Depth-first Search Each vertex is initially white It is grayed when it is  discovered  in the search and is blackened when it is  finished , that is, when its adjacency list has been examined completely. This technique guarantees that each vertex ends up in exactly one depth-first tree, so that these trees are disjoint.
Depth-first Search Each vertex  v  has two timestamps: the first timestamp  d [ v ] records when  v  is first discovered (and grayed), and the second timestamp  f  [ v ] records when the search finishes examining  v 's adjacency list (and blackens  v ).
Copyright  © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright  © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright  © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Property of Depth-First search An important property of depth-first search is that discovery and finishing times have  parenthesis structure .
Copyright  © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Classification of Edges We can define four edge types in terms of the depth-first forest  Gπ  produced by a depth-first search on  G . 1.  Tree edges  are edges in the depth-first forest  Gπ . Edge ( u ,  v ) is a tree edge if  v  was first discovered by exploring edge ( u ,  v ). 2.  Back edges  are those edges ( u ,  v ) connecting a vertex  u  to an ancestor  v  in a depth first tree. Self-loops, which may occur in directed graphs, are considered to be back edges.
Classification of Edges 3.  Forward edges  are those non-tree edges ( u ,  v ) connecting a vertex  u  to a descendant  v  in a depth-first tree. 4.  Cross edges  are all other edges.
Classification of Edges The DFS algorithm can be modified to classify edges as it encounters them. The key idea is that each edge ( u ,  v ) can be classified by the color of the vertex  v  that is reached when the edge is first explored . 1. WHITE indicates a tree edge, 2. GRAY indicates a back edge, and 3. BLACK indicates a forward or cross edge.
Topological Sort A topological sort of a graph can be viewed as an ordering of its vertices along a horizontal line so that all directed edges go from left to right.
Copyright  © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Copyright  © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Strongly Connected Components A strongly connected component of a directed graph G=(V,E) is a maximal set of vertices  such that for every pair of vertices u and v in C , we have both u ~v and v ~u
Copyright  © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Strongly Connected Components The transpose of a graph G, G T =(V,E T )  is used for finding the strongly connected components of a graph G=(V,E) It is interesting to observe that G and G T  have exactly the same strongly connected components
Copyright  © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Ad

More Related Content

What's hot (20)

Dijkstra algorithm a dynammic programming approach
Dijkstra algorithm   a dynammic programming approachDijkstra algorithm   a dynammic programming approach
Dijkstra algorithm a dynammic programming approach
Akash Sethiya
 
Algoritma Record
Algoritma RecordAlgoritma Record
Algoritma Record
brigidaarie
 
Automata theory - NFA to DFA Conversion
Automata theory - NFA to DFA ConversionAutomata theory - NFA to DFA Conversion
Automata theory - NFA to DFA Conversion
Akila Krishnamoorthy
 
Network flows
Network flowsNetwork flows
Network flows
Richa Bandlas
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
Madhu Bala
 
RABIN KARP ALGORITHM STRING MATCHING
RABIN KARP ALGORITHM STRING MATCHINGRABIN KARP ALGORITHM STRING MATCHING
RABIN KARP ALGORITHM STRING MATCHING
Abhishek Singh
 
Road Map and OUtlines BS(CS) 2019-23.pdf
Road Map and OUtlines BS(CS) 2019-23.pdfRoad Map and OUtlines BS(CS) 2019-23.pdf
Road Map and OUtlines BS(CS) 2019-23.pdf
NoorFatima504746
 
18 Basic Graph Algorithms
18 Basic Graph Algorithms18 Basic Graph Algorithms
18 Basic Graph Algorithms
Andres Mendez-Vazquez
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
Masud Parvaze
 
Module 2
Module 2Module 2
Module 2
UllasSS1
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
Omprakash Chauhan
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
Emertxe Information Technologies Pvt Ltd
 
Sub matrices - Circuit Matrix
Sub matrices - Circuit MatrixSub matrices - Circuit Matrix
Sub matrices - Circuit Matrix
AditiAgrawal588151
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 
Chapter 5 Image Processing: Fourier Transformation
Chapter 5 Image Processing: Fourier TransformationChapter 5 Image Processing: Fourier Transformation
Chapter 5 Image Processing: Fourier Transformation
Varun Ojha
 
Set data structure
Set data structure Set data structure
Set data structure
Tech_MX
 
Software Testing & Quality Assurance- Black-Box Testing
Software Testing & Quality Assurance- Black-Box TestingSoftware Testing & Quality Assurance- Black-Box Testing
Software Testing & Quality Assurance- Black-Box Testing
Minhas Kamal
 
Binary search trees
Binary search treesBinary search trees
Binary search trees
Dhananjaysinh Jhala
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
Shuvongkor Barman
 
Dijkstra algorithm a dynammic programming approach
Dijkstra algorithm   a dynammic programming approachDijkstra algorithm   a dynammic programming approach
Dijkstra algorithm a dynammic programming approach
Akash Sethiya
 
Algoritma Record
Algoritma RecordAlgoritma Record
Algoritma Record
brigidaarie
 
Automata theory - NFA to DFA Conversion
Automata theory - NFA to DFA ConversionAutomata theory - NFA to DFA Conversion
Automata theory - NFA to DFA Conversion
Akila Krishnamoorthy
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
Madhu Bala
 
RABIN KARP ALGORITHM STRING MATCHING
RABIN KARP ALGORITHM STRING MATCHINGRABIN KARP ALGORITHM STRING MATCHING
RABIN KARP ALGORITHM STRING MATCHING
Abhishek Singh
 
Road Map and OUtlines BS(CS) 2019-23.pdf
Road Map and OUtlines BS(CS) 2019-23.pdfRoad Map and OUtlines BS(CS) 2019-23.pdf
Road Map and OUtlines BS(CS) 2019-23.pdf
NoorFatima504746
 
Stack - Data Structure - Notes
Stack - Data Structure - NotesStack - Data Structure - Notes
Stack - Data Structure - Notes
Omprakash Chauhan
 
Chapter 5 Image Processing: Fourier Transformation
Chapter 5 Image Processing: Fourier TransformationChapter 5 Image Processing: Fourier Transformation
Chapter 5 Image Processing: Fourier Transformation
Varun Ojha
 
Set data structure
Set data structure Set data structure
Set data structure
Tech_MX
 
Software Testing & Quality Assurance- Black-Box Testing
Software Testing & Quality Assurance- Black-Box TestingSoftware Testing & Quality Assurance- Black-Box Testing
Software Testing & Quality Assurance- Black-Box Testing
Minhas Kamal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
Shuvongkor Barman
 

Viewers also liked (20)

Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
Alizay Khan
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
Ankit Kumar Singh
 
DFS and BFS
DFS and BFSDFS and BFS
DFS and BFS
satya parsana
 
Graphs bfs dfs
Graphs bfs dfsGraphs bfs dfs
Graphs bfs dfs
Jaya Gautam
 
BFS
BFSBFS
BFS
jyothimonc
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
Amrinder Arora
 
Breadth first search
Breadth first searchBreadth first search
Breadth first search
Vignesh Prasanna
 
(Radhika) presentation on chapter 2 ai
(Radhika) presentation on chapter 2 ai(Radhika) presentation on chapter 2 ai
(Radhika) presentation on chapter 2 ai
Radhika Srinivasan
 
DFS & BFS Graph
DFS & BFS GraphDFS & BFS Graph
DFS & BFS Graph
Mahfuzul Yamin
 
Depth First Search, Breadth First Search and Best First Search
Depth First Search, Breadth First Search and Best First SearchDepth First Search, Breadth First Search and Best First Search
Depth First Search, Breadth First Search and Best First Search
Adri Jovin
 
chapter 1
chapter 1chapter 1
chapter 1
yatheesha
 
Bfs dfs
Bfs dfsBfs dfs
Bfs dfs
Praveen Yadav
 
NUMA optimized Parallel Breadth first Search on Multicore Single node System
NUMA optimized Parallel Breadth first Search on Multicore Single node SystemNUMA optimized Parallel Breadth first Search on Multicore Single node System
NUMA optimized Parallel Breadth first Search on Multicore Single node System
Mohammad Tahsin Alshalabi
 
11. dfs
11. dfs11. dfs
11. dfs
Dr Sandeep Kumar Poonia
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Ehtisham Ali
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
Krish_ver2
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
oneous
 
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part II
Amrinder Arora
 
Heuristic Search
Heuristic SearchHeuristic Search
Heuristic Search
butest
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
Alizay Khan
 
Bfs and dfs in data structure
Bfs and dfs in  data structure Bfs and dfs in  data structure
Bfs and dfs in data structure
Ankit Kumar Singh
 
Introduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic NotationIntroduction to Algorithms and Asymptotic Notation
Introduction to Algorithms and Asymptotic Notation
Amrinder Arora
 
(Radhika) presentation on chapter 2 ai
(Radhika) presentation on chapter 2 ai(Radhika) presentation on chapter 2 ai
(Radhika) presentation on chapter 2 ai
Radhika Srinivasan
 
Depth First Search, Breadth First Search and Best First Search
Depth First Search, Breadth First Search and Best First SearchDepth First Search, Breadth First Search and Best First Search
Depth First Search, Breadth First Search and Best First Search
Adri Jovin
 
NUMA optimized Parallel Breadth first Search on Multicore Single node System
NUMA optimized Parallel Breadth first Search on Multicore Single node SystemNUMA optimized Parallel Breadth first Search on Multicore Single node System
NUMA optimized Parallel Breadth first Search on Multicore Single node System
Mohammad Tahsin Alshalabi
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Ehtisham Ali
 
2.5 bfs & dfs 02
2.5 bfs & dfs 022.5 bfs & dfs 02
2.5 bfs & dfs 02
Krish_ver2
 
Prim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning treePrim's Algorithm on minimum spanning tree
Prim's Algorithm on minimum spanning tree
oneous
 
Dynamic Programming - Part II
Dynamic Programming - Part IIDynamic Programming - Part II
Dynamic Programming - Part II
Amrinder Arora
 
Heuristic Search
Heuristic SearchHeuristic Search
Heuristic Search
butest
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
Ad

Similar to chapter22.ppt (11)

Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
Hossain Md Shakhawat
 
chapter23.ppt
chapter23.pptchapter23.ppt
chapter23.ppt
Tareq Hasan
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
whittemorelucilla
 
DFS ppt.pdf
DFS ppt.pdfDFS ppt.pdf
DFS ppt.pdf
Rajkk5
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
DhruvilSTATUS
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
DEEPIKA T
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
king779879
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structure
Ikhlas Rahman
 
spanning_tree ssw descreate mathematics.pdf
spanning_tree ssw descreate mathematics.pdfspanning_tree ssw descreate mathematics.pdf
spanning_tree ssw descreate mathematics.pdf
moslahuddin2022
 
graph representation.pdf
graph representation.pdfgraph representation.pdf
graph representation.pdf
amitbhachne
 
DFS.pdf
DFS.pdfDFS.pdf
DFS.pdf
jayarao21
 
Breadth first search and depth first search
Breadth first search and  depth first searchBreadth first search and  depth first search
Breadth first search and depth first search
Hossain Md Shakhawat
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docxgraphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
whittemorelucilla
 
DFS ppt.pdf
DFS ppt.pdfDFS ppt.pdf
DFS ppt.pdf
Rajkk5
 
Unit-6 Graph.ppsx ppt
Unit-6 Graph.ppsx                                       pptUnit-6 Graph.ppsx                                       ppt
Unit-6 Graph.ppsx ppt
DhruvilSTATUS
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
DEEPIKA T
 
Lecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptxLecture 2.3.1 Graph.pptx
Lecture 2.3.1 Graph.pptx
king779879
 
Graph Basic In Data structure
Graph Basic In Data structureGraph Basic In Data structure
Graph Basic In Data structure
Ikhlas Rahman
 
spanning_tree ssw descreate mathematics.pdf
spanning_tree ssw descreate mathematics.pdfspanning_tree ssw descreate mathematics.pdf
spanning_tree ssw descreate mathematics.pdf
moslahuddin2022
 
graph representation.pdf
graph representation.pdfgraph representation.pdf
graph representation.pdf
amitbhachne
 
Ad

More from Tareq Hasan (20)

Grow Your Career with WordPress
Grow Your Career with WordPressGrow Your Career with WordPress
Grow Your Career with WordPress
Tareq Hasan
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPress
Tareq Hasan
 
How to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org RepositoryHow to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org Repository
Tareq Hasan
 
Composer - The missing package manager for PHP
Composer - The missing package manager for PHPComposer - The missing package manager for PHP
Composer - The missing package manager for PHP
Tareq Hasan
 
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
Tareq Hasan
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array Pointer
Tareq Hasan
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
Tareq Hasan
 
chapter - 6.ppt
chapter - 6.pptchapter - 6.ppt
chapter - 6.ppt
Tareq Hasan
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
Tareq Hasan
 
chapter-8.ppt
chapter-8.pptchapter-8.ppt
chapter-8.ppt
Tareq Hasan
 
chapter24.ppt
chapter24.pptchapter24.ppt
chapter24.ppt
Tareq Hasan
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
Tareq Hasan
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
Tareq Hasan
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
Tareq Hasan
 
Java: Exception
Java: ExceptionJava: Exception
Java: Exception
Tareq Hasan
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
Tareq Hasan
 
Java: Class Design Examples
Java: Class Design ExamplesJava: Class Design Examples
Java: Class Design Examples
Tareq Hasan
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
Tareq Hasan
 
Grow Your Career with WordPress
Grow Your Career with WordPressGrow Your Career with WordPress
Grow Your Career with WordPress
Tareq Hasan
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPress
Tareq Hasan
 
How to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org RepositoryHow to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org Repository
Tareq Hasan
 
Composer - The missing package manager for PHP
Composer - The missing package manager for PHPComposer - The missing package manager for PHP
Composer - The missing package manager for PHP
Tareq Hasan
 
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
Tareq Hasan
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
Tareq Hasan
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array Pointer
Tareq Hasan
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
Tareq Hasan
 
Algorithm: priority queue
Algorithm: priority queueAlgorithm: priority queue
Algorithm: priority queue
Tareq Hasan
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
Tareq Hasan
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
Tareq Hasan
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
Tareq Hasan
 
Java: Class Design Examples
Java: Class Design ExamplesJava: Class Design Examples
Java: Class Design Examples
Tareq Hasan
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
Tareq Hasan
 

chapter22.ppt

  • 1. Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22
  • 2. Graph Two standard ways to represent a graph Adjacency List Adjacency Matrix
  • 3. Adjacency List Representation of a Graph For each u є V , the adjacency list Adj [ u ] contains all the vertices v such that there is an edge ( u , v ) є E .
  • 4. Adjacency Matrix Representation of a Graph The adjacency-matrix representation of a graph G consists of a | V | × | V | matrix A = ( aij ) such that a ij =1 if (i,j) є E =o otherwise
  • 5. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 6. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 7. Breath-first Search Breadth-first search is one of the simplest algorithms for searching a graph Given a graph G = ( V , E ) and a distinguished source vertex s , breadth-first search systematically explores the edges of G to "discover" every vertex that is reachable from s .
  • 8. Breath-first Search T he algorithm discovers all vertices at distance k from s before discovering any vertices at distance k + 1.
  • 9. Breath-first Search To keep track of progress, breadth-first search colors each vertex white, gray, or black. All vertices start out white and may later become gray and then black. A vertex is discovered the first time it is encountered during the search, at which time it becomes nonwhite.
  • 10. Breath-first Search The color of each vertex u is stored in the variable color [ u ]. The predecessor of u is stored in the variable π[ u ]. The distance from the source s to vertex u computed by the algorithm is stored in d [ u ]
  • 11. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 12. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 13. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 14. Depth-first Search The strategy followed by depth-first search is, as its name implies, to search "deeper" in the graph whenever possible.
  • 15. Depth-first Search The predecessor sub-graph of a depth-first search forms a depth-first forest composed of several depth-first trees . The edges in Eπ are called tree edges .
  • 16. Depth-first Search Each vertex is initially white It is grayed when it is discovered in the search and is blackened when it is finished , that is, when its adjacency list has been examined completely. This technique guarantees that each vertex ends up in exactly one depth-first tree, so that these trees are disjoint.
  • 17. Depth-first Search Each vertex v has two timestamps: the first timestamp d [ v ] records when v is first discovered (and grayed), and the second timestamp f [ v ] records when the search finishes examining v 's adjacency list (and blackens v ).
  • 18. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 19. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 20. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 21. Property of Depth-First search An important property of depth-first search is that discovery and finishing times have parenthesis structure .
  • 22. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 23. Classification of Edges We can define four edge types in terms of the depth-first forest Gπ produced by a depth-first search on G . 1. Tree edges are edges in the depth-first forest Gπ . Edge ( u , v ) is a tree edge if v was first discovered by exploring edge ( u , v ). 2. Back edges are those edges ( u , v ) connecting a vertex u to an ancestor v in a depth first tree. Self-loops, which may occur in directed graphs, are considered to be back edges.
  • 24. Classification of Edges 3. Forward edges are those non-tree edges ( u , v ) connecting a vertex u to a descendant v in a depth-first tree. 4. Cross edges are all other edges.
  • 25. Classification of Edges The DFS algorithm can be modified to classify edges as it encounters them. The key idea is that each edge ( u , v ) can be classified by the color of the vertex v that is reached when the edge is first explored . 1. WHITE indicates a tree edge, 2. GRAY indicates a back edge, and 3. BLACK indicates a forward or cross edge.
  • 26. Topological Sort A topological sort of a graph can be viewed as an ordering of its vertices along a horizontal line so that all directed edges go from left to right.
  • 27. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 28. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 29. Strongly Connected Components A strongly connected component of a directed graph G=(V,E) is a maximal set of vertices such that for every pair of vertices u and v in C , we have both u ~v and v ~u
  • 30. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
  • 31. Strongly Connected Components The transpose of a graph G, G T =(V,E T ) is used for finding the strongly connected components of a graph G=(V,E) It is interesting to observe that G and G T have exactly the same strongly connected components
  • 32. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.