SlideShare a Scribd company logo
Lecture 12:
      Depth-First Search
         Steven Skiena

Department of Computer Science
 State University of New York
 Stony Brook, NY 11794–4400

http://www.cs.sunysb.edu/∼skiena
Problem of the Day
Prove that in a breadth-first search on a undirected graph G,
every edge in G is either a tree edge or a cross edge, where a
cross edge (x, y) is an edge where x is neither is an ancestor
or descendent of y.
Depth-First Search
DFS has a neat recursive implementation which eliminates
the need to explicitly use a stack.
Discovery and final times are a convenience to maintain.
dfs(graph *g, int v)
{
      edgenode *p; (* temporary pointer *)
      int y; (* successor vertex *)

     if (finished) return; (* allow for search termination *)

     discovered[v] = TRUE;
     time = time + 1;
     entry time[v] = time;

     process vertex early(v);

     p = g− >edges[v];
     while (p ! = NULL) {
           y = p− >y;
if (discovered[y] == FALSE) {
                 parent[y] = v;
                 process edge(v,y);
                 dfs(g,y);
          }
          else if ((!processed[y]) || (g− >directed))
                 process edge(v,y);

          if (finished) return;

          p = p− >next;
    }

    process vertex late(v);

    time = time + 1;
    exit time[v] = time;

    processed[v] = TRUE;
}
The Key Idea with DFS
A depth-first search of a graph organizes the edges of the
graph in a precise way.
In a DFS of an undirected graph, we assign a direction to each
edge, from the vertex which discover it:
                                            2
                            1                       3

                                    1
                        2       6
                                                        4

                    3
                                                5

                    4                   6



                     5
Edge Classification for DFS
Every edge is either:
                               3. A Forward Edge
    1. A Tree Edge                to a decendant




                               4. A Cross Edge
    2. A Back Edge
                                  to a different node
       to an ancestor




On any particular DFS or BFS of a directed or undirected
graph, each edge gets classified as one of the above.
Edge Classification Implementation

int edge classification(int x, int y)
{
      if (parent[y] == x) return(TREE);
      if (discovered[y] && !processed[y]) return(BACK);
      if (processed[y] && (entry time[y]¿entry time[x])) return(FORWARD);
      if (processed[y] && (entry time[y]¡entry time[x])) return(CROSS);

     printf(”Warning: unclassified edge (%d,%d)”,x,y);
}
DFS: Tree Edges and Back Edges Only
The reason DFS is so important is that it defines a very nice
ordering to the edges of the graph.
In a DFS of an undirected graph, every edge is either a tree
edge or a back edge.
Why? Suppose we have a forward edge. We would have
encountered (4, 1) when expanding 4, so this is a back edge.
                               1

                           2

                       3            4
No Cross Edges in DFS
Suppose we have a cross-edge
                        1


                    2           5   When expanding 2, we would discover
                                    5, so the tree would look like:


                3       4   6                  1


                                           2


                                    3
                                           4       5



                                                           6
DFS Application: Finding Cycles
Back edges are the key to finding a cycle in an undirected
graph.
Any back edge going from x to an ancestor y creates a cycle
with the path in the tree from y to x.
process edge(int x, int y)
{
      if (parent[x] ! = y) { (* found back edge! *)
             printf(”Cycle from %d to %d:”,y,x);
             find path(y,x,parent);
             finished = TRUE;
      }
}
Articulation Vertices
Suppose you are a terrorist, seeking to disrupt the telephone
network. Which station do you blow up?



An articulation vertex is a vertex of a connected graph whose
deletion disconnects the graph.
Clearly connectivity is an important concern in the design of
any network.
Articulation vertices can be found in O(n(m+n)) – just delete
each vertex to do a DFS on the remaining graph to see if it is
connected.
A Faster O(n + m) DFS Algorithm
In a DFS tree, a vertex v (other than the root) is an articulation
vertex iff v is not a leaf and some subtree of v has no back
edge incident until a proper ancestor of v.
                                            The root is a special case since
                                            it has no ancestors.
                           X
                                            X is an articulation vertex since
                                            the right subtree does not have
                                            a back edge to a proper ancestor.




                    Leaves cannot be
                    articulation vertices
Topological Sorting
A directed, acyclic graph has no directed cycles.
                             B               D


                      A                  E
                                 C



                      G              F




A topological sort of a graph is an ordering on the vertices so
that all edges go from left to right.
DAGs (and only DAGs) has at least one topological sort (here
G, A, B, C, F, E, D).
Applications of Topological Sorting
Topological sorting is often useful in scheduling jobs in their
proper sequence. In general, we can use it to order things
given precidence constraints.
Example: Dressing schedule from CLR.
Example: Identifying errors in DNA fragment
assembly
Certain fragments are constrained to be to the left or right of
other fragments, unless there are errors.
                                    A B R A C A D A B R A
                A   B   R   A   C
                                    A B R A C
                A   C   A   D   A       R A C A D
                A   D   A   B   R         A C A D A
                D   A   B   R   A             A D A B R
                R   A   C   A   D               D A B R A


Solution – build a DAG representing all the left-right
constraints. Any topological sort of this DAG is a consistant
ordering. If there are cycles, there must be errors.
Topological Sorting via DFS
A directed graph is a DAG if and only if no back edges are
encountered during a depth-first search.
Labeling each of the vertices in the reverse order that they are
marked processed finds a topological sort of a DAG.
Why? Consider what happens to each directed edge {x, y} as
we encounter it during the exploration of vertex x:
Case Analysis

 • If y is currently undiscovered, then we then start a DFS
   of y before we can continue with x. Thus y is marked
   completed before x is, and x appears before y in the
   topological order, as it must.
 • If y is discovered but not completed, then {x, y} is a back
   edge, which is forbidden in a DAG.
 • If y is completed, then it will have been so labeled before
   x. Therefore, x appears before y in the topological order,
   as it must.
Topological Sorting Implementation
process vertex late(int v)
{
      push(&sorted,v);
}


process edge(int x, int y)
{
      int class;

      class = edge classification(x,y);

      if (class == BACK)
             printf(”Warning: directed cycle found, not a DAG”);
}

We push each vertex on a stack soon as we have evaluated
all outgoing edges. The top vertex on the stack always has
no incoming edges from any vertex on the stack, repeatedly
popping them off yields a topological ordering.
Strongly Connected Components
A directed graph is strongly connected iff there is a directed
path between any two vertices.
The strongly connected components of a graph is a partition
of the vertices into subsets (maximal) such that each subset is
strongly connected.
                   a       b       e         f




                   c           d   g     h




Observe that no vertex can be in two maximal components,
so it is a partition.
There is an elegant, linear time algorithm to find the strongly
connected components of a directed graph using DFS which
is similar to the algorithm for biconnected components.
Backtracking and Depth-First Search
Depth-first search uses essentially the same idea as backtrack-
ing.
Both involve exhaustively searching all possibilities by
advancing if it is possible, and backing up as soon as there
is no unexplored possibility for further advancement. Both
are most easily understood as recursive algorithms.

More Related Content

What's hot (20)

PPT
Biconnected components (13024116056)
Akshay soni
 
PPT
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Mohanlal Sukhadia University (MLSU)
 
PPT
2.5 graph dfs
Krish_ver2
 
PDF
All pairs shortest path algorithm
Srikrishnan Suresh
 
PPT
Shortest path
Farah Shaikh
 
PPTX
BFS
jyothimonc
 
PPTX
Graph Algorithms: Breadth-First Search (BFS)
Md. Shafiuzzaman Hira
 
PPTX
(floyd's algm)
Jothi Lakshmi
 
PPT
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
PPT
Dijksatra
Tanmay Baranwal
 
PPTX
Shortest path algorithm
sana younas
 
PPT
bode_plot By DEV
Devchandra Thakur
 
DOC
BFS, Breadth first search | Search Traversal Algorithm
MSA Technosoft
 
PPT
Bellman Ford's Algorithm
Tanmay Baranwal
 
PPTX
Presentation on Breadth First Search (BFS)
Shuvongkor Barman
 
PPTX
Graph Traversal Algorithms - Depth First Search Traversal
Amrinder Arora
 
PPTX
Graph Algorithms
Ashwin Shiv
 
PDF
Skiena algorithm 2007 lecture11 breadth deapth first search
zukun
 
PDF
18 Basic Graph Algorithms
Andres Mendez-Vazquez
 
PPT
The Floyd–Warshall algorithm
José Juan Herrera
 
Biconnected components (13024116056)
Akshay soni
 
Shortest path (Dijkistra's Algorithm) & Spanning Tree (Prim's Algorithm)
Mohanlal Sukhadia University (MLSU)
 
2.5 graph dfs
Krish_ver2
 
All pairs shortest path algorithm
Srikrishnan Suresh
 
Shortest path
Farah Shaikh
 
Graph Algorithms: Breadth-First Search (BFS)
Md. Shafiuzzaman Hira
 
(floyd's algm)
Jothi Lakshmi
 
SINGLE-SOURCE SHORTEST PATHS
Md. Shafiuzzaman Hira
 
Dijksatra
Tanmay Baranwal
 
Shortest path algorithm
sana younas
 
bode_plot By DEV
Devchandra Thakur
 
BFS, Breadth first search | Search Traversal Algorithm
MSA Technosoft
 
Bellman Ford's Algorithm
Tanmay Baranwal
 
Presentation on Breadth First Search (BFS)
Shuvongkor Barman
 
Graph Traversal Algorithms - Depth First Search Traversal
Amrinder Arora
 
Graph Algorithms
Ashwin Shiv
 
Skiena algorithm 2007 lecture11 breadth deapth first search
zukun
 
18 Basic Graph Algorithms
Andres Mendez-Vazquez
 
The Floyd–Warshall algorithm
José Juan Herrera
 

Viewers also liked (6)

PPT
2.2 topological sort 02
Krish_ver2
 
PPTX
Topological sort
stella D
 
PPT
Graph algorithm
University of Potsdam
 
PPT
Ch18
leminhvuong
 
PDF
Bloom filter
Hamid Feizabadi
 
PDF
Skiena algorithm 2007 lecture10 graph data strctures
zukun
 
2.2 topological sort 02
Krish_ver2
 
Topological sort
stella D
 
Graph algorithm
University of Potsdam
 
Bloom filter
Hamid Feizabadi
 
Skiena algorithm 2007 lecture10 graph data strctures
zukun
 
Ad

Similar to Skiena algorithm 2007 lecture12 topological sort connectivity (20)

PPT
Graph
Shakil Ahmed
 
PPT
ALG5.1.pptdsfnj,sdhfjk hsdjkfhsdjkfhj ksd hfjksdhfjksd
robozenbd
 
PPTX
Unit 4-PartB of data design and algorithms
cartoongamingworld09
 
DOCX
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
whittemorelucilla
 
PPTX
Graphs
KomalPaliwal3
 
PPTX
Graph Traversing and Seaching - Data Structure- AIUB.pptx
Surid2
 
PPTX
temp.pptxsusueueuune ueieoekeebhe ejene h
harshitou754
 
PPTX
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
PPT
ALG5.1.ppt
AbhinavAshish17
 
PPT
gSpan algorithm
Sadik Mussah
 
PPT
gSpan algorithm
Sadik Mussah
 
PPT
2.5 bfs & dfs 02
Krish_ver2
 
PPTX
Breath first Search and Depth first search
Kirti Verma
 
PPTX
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
AzwanAlghifari
 
PDF
LEC 12-DSALGO-GRAPHS(final12).pdf
MuhammadUmerIhtisham
 
PPTX
Data Structures and Agorithm: DS 21 Graph Theory.pptx
RashidFaridChishti
 
PPTX
DFS and BFS
satya parsana
 
PPTX
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
 
PPTX
CPE-121-Discrete-Math_Graph-Theory..pptx
RyoOlaybar
 
ALG5.1.pptdsfnj,sdhfjk hsdjkfhsdjkfhj ksd hfjksdhfjksd
robozenbd
 
Unit 4-PartB of data design and algorithms
cartoongamingworld09
 
graphin-c1.pnggraphin-c1.txt1 22 3 83 44 5.docx
whittemorelucilla
 
Graph Traversing and Seaching - Data Structure- AIUB.pptx
Surid2
 
temp.pptxsusueueuune ueieoekeebhe ejene h
harshitou754
 
Depth First Search Algorithm in 🧠 DFS in Artificial Intelligence (AI)
rafeeqshaik212002
 
ALG5.1.ppt
AbhinavAshish17
 
gSpan algorithm
Sadik Mussah
 
gSpan algorithm
Sadik Mussah
 
2.5 bfs & dfs 02
Krish_ver2
 
Breath first Search and Depth first search
Kirti Verma
 
algoritmagraph_breadthfirstsearch_depthfirstsearch.pptx
AzwanAlghifari
 
LEC 12-DSALGO-GRAPHS(final12).pdf
MuhammadUmerIhtisham
 
Data Structures and Agorithm: DS 21 Graph Theory.pptx
RashidFaridChishti
 
DFS and BFS
satya parsana
 
Data Structures - Lecture 10 [Graphs]
Muhammad Hammad Waseem
 
CPE-121-Discrete-Math_Graph-Theory..pptx
RyoOlaybar
 
Ad

More from zukun (20)

PDF
My lyn tutorial 2009
zukun
 
PDF
ETHZ CV2012: Tutorial openCV
zukun
 
PDF
ETHZ CV2012: Information
zukun
 
PDF
Siwei lyu: natural image statistics
zukun
 
PDF
Lecture9 camera calibration
zukun
 
PDF
Brunelli 2008: template matching techniques in computer vision
zukun
 
PDF
Modern features-part-4-evaluation
zukun
 
PDF
Modern features-part-3-software
zukun
 
PDF
Modern features-part-2-descriptors
zukun
 
PDF
Modern features-part-1-detectors
zukun
 
PDF
Modern features-part-0-intro
zukun
 
PDF
Lecture 02 internet video search
zukun
 
PDF
Lecture 01 internet video search
zukun
 
PDF
Lecture 03 internet video search
zukun
 
PDF
Icml2012 tutorial representation_learning
zukun
 
PPT
Advances in discrete energy minimisation for computer vision
zukun
 
PDF
Gephi tutorial: quick start
zukun
 
PDF
EM algorithm and its application in probabilistic latent semantic analysis
zukun
 
PDF
Object recognition with pictorial structures
zukun
 
PDF
Iccv2011 learning spatiotemporal graphs of human activities
zukun
 
My lyn tutorial 2009
zukun
 
ETHZ CV2012: Tutorial openCV
zukun
 
ETHZ CV2012: Information
zukun
 
Siwei lyu: natural image statistics
zukun
 
Lecture9 camera calibration
zukun
 
Brunelli 2008: template matching techniques in computer vision
zukun
 
Modern features-part-4-evaluation
zukun
 
Modern features-part-3-software
zukun
 
Modern features-part-2-descriptors
zukun
 
Modern features-part-1-detectors
zukun
 
Modern features-part-0-intro
zukun
 
Lecture 02 internet video search
zukun
 
Lecture 01 internet video search
zukun
 
Lecture 03 internet video search
zukun
 
Icml2012 tutorial representation_learning
zukun
 
Advances in discrete energy minimisation for computer vision
zukun
 
Gephi tutorial: quick start
zukun
 
EM algorithm and its application in probabilistic latent semantic analysis
zukun
 
Object recognition with pictorial structures
zukun
 
Iccv2011 learning spatiotemporal graphs of human activities
zukun
 

Recently uploaded (20)

PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Français Patch Tuesday - Juillet
Ivanti
 
PDF
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
PDF
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Persuasive AI: risks and opportunities in the age of digital debate
Speck&Tech
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Français Patch Tuesday - Juillet
Ivanti
 
Rethinking Security Operations - SOC Evolution Journey.pdf
Haris Chughtai
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Empowering Cloud Providers with Apache CloudStack and Stackbill
ShapeBlue
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 

Skiena algorithm 2007 lecture12 topological sort connectivity

  • 1. Lecture 12: Depth-First Search Steven Skiena Department of Computer Science State University of New York Stony Brook, NY 11794–4400 http://www.cs.sunysb.edu/∼skiena
  • 2. Problem of the Day Prove that in a breadth-first search on a undirected graph G, every edge in G is either a tree edge or a cross edge, where a cross edge (x, y) is an edge where x is neither is an ancestor or descendent of y.
  • 3. Depth-First Search DFS has a neat recursive implementation which eliminates the need to explicitly use a stack. Discovery and final times are a convenience to maintain. dfs(graph *g, int v) { edgenode *p; (* temporary pointer *) int y; (* successor vertex *) if (finished) return; (* allow for search termination *) discovered[v] = TRUE; time = time + 1; entry time[v] = time; process vertex early(v); p = g− >edges[v]; while (p ! = NULL) { y = p− >y;
  • 4. if (discovered[y] == FALSE) { parent[y] = v; process edge(v,y); dfs(g,y); } else if ((!processed[y]) || (g− >directed)) process edge(v,y); if (finished) return; p = p− >next; } process vertex late(v); time = time + 1; exit time[v] = time; processed[v] = TRUE; }
  • 5. The Key Idea with DFS A depth-first search of a graph organizes the edges of the graph in a precise way. In a DFS of an undirected graph, we assign a direction to each edge, from the vertex which discover it: 2 1 3 1 2 6 4 3 5 4 6 5
  • 6. Edge Classification for DFS Every edge is either: 3. A Forward Edge 1. A Tree Edge to a decendant 4. A Cross Edge 2. A Back Edge to a different node to an ancestor On any particular DFS or BFS of a directed or undirected graph, each edge gets classified as one of the above.
  • 7. Edge Classification Implementation int edge classification(int x, int y) { if (parent[y] == x) return(TREE); if (discovered[y] && !processed[y]) return(BACK); if (processed[y] && (entry time[y]¿entry time[x])) return(FORWARD); if (processed[y] && (entry time[y]¡entry time[x])) return(CROSS); printf(”Warning: unclassified edge (%d,%d)”,x,y); }
  • 8. DFS: Tree Edges and Back Edges Only The reason DFS is so important is that it defines a very nice ordering to the edges of the graph. In a DFS of an undirected graph, every edge is either a tree edge or a back edge. Why? Suppose we have a forward edge. We would have encountered (4, 1) when expanding 4, so this is a back edge. 1 2 3 4
  • 9. No Cross Edges in DFS Suppose we have a cross-edge 1 2 5 When expanding 2, we would discover 5, so the tree would look like: 3 4 6 1 2 3 4 5 6
  • 10. DFS Application: Finding Cycles Back edges are the key to finding a cycle in an undirected graph. Any back edge going from x to an ancestor y creates a cycle with the path in the tree from y to x. process edge(int x, int y) { if (parent[x] ! = y) { (* found back edge! *) printf(”Cycle from %d to %d:”,y,x); find path(y,x,parent); finished = TRUE; } }
  • 11. Articulation Vertices Suppose you are a terrorist, seeking to disrupt the telephone network. Which station do you blow up? An articulation vertex is a vertex of a connected graph whose deletion disconnects the graph. Clearly connectivity is an important concern in the design of any network. Articulation vertices can be found in O(n(m+n)) – just delete each vertex to do a DFS on the remaining graph to see if it is connected.
  • 12. A Faster O(n + m) DFS Algorithm In a DFS tree, a vertex v (other than the root) is an articulation vertex iff v is not a leaf and some subtree of v has no back edge incident until a proper ancestor of v. The root is a special case since it has no ancestors. X X is an articulation vertex since the right subtree does not have a back edge to a proper ancestor. Leaves cannot be articulation vertices
  • 13. Topological Sorting A directed, acyclic graph has no directed cycles. B D A E C G F A topological sort of a graph is an ordering on the vertices so that all edges go from left to right. DAGs (and only DAGs) has at least one topological sort (here G, A, B, C, F, E, D).
  • 14. Applications of Topological Sorting Topological sorting is often useful in scheduling jobs in their proper sequence. In general, we can use it to order things given precidence constraints. Example: Dressing schedule from CLR.
  • 15. Example: Identifying errors in DNA fragment assembly Certain fragments are constrained to be to the left or right of other fragments, unless there are errors. A B R A C A D A B R A A B R A C A B R A C A C A D A R A C A D A D A B R A C A D A D A B R A A D A B R R A C A D D A B R A Solution – build a DAG representing all the left-right constraints. Any topological sort of this DAG is a consistant ordering. If there are cycles, there must be errors.
  • 16. Topological Sorting via DFS A directed graph is a DAG if and only if no back edges are encountered during a depth-first search. Labeling each of the vertices in the reverse order that they are marked processed finds a topological sort of a DAG. Why? Consider what happens to each directed edge {x, y} as we encounter it during the exploration of vertex x:
  • 17. Case Analysis • If y is currently undiscovered, then we then start a DFS of y before we can continue with x. Thus y is marked completed before x is, and x appears before y in the topological order, as it must. • If y is discovered but not completed, then {x, y} is a back edge, which is forbidden in a DAG. • If y is completed, then it will have been so labeled before x. Therefore, x appears before y in the topological order, as it must.
  • 18. Topological Sorting Implementation process vertex late(int v) { push(&sorted,v); } process edge(int x, int y) { int class; class = edge classification(x,y); if (class == BACK) printf(”Warning: directed cycle found, not a DAG”); } We push each vertex on a stack soon as we have evaluated all outgoing edges. The top vertex on the stack always has no incoming edges from any vertex on the stack, repeatedly popping them off yields a topological ordering.
  • 19. Strongly Connected Components A directed graph is strongly connected iff there is a directed path between any two vertices. The strongly connected components of a graph is a partition of the vertices into subsets (maximal) such that each subset is strongly connected. a b e f c d g h Observe that no vertex can be in two maximal components, so it is a partition.
  • 20. There is an elegant, linear time algorithm to find the strongly connected components of a directed graph using DFS which is similar to the algorithm for biconnected components.
  • 21. Backtracking and Depth-First Search Depth-first search uses essentially the same idea as backtrack- ing. Both involve exhaustively searching all possibilities by advancing if it is possible, and backing up as soon as there is no unexplored possibility for further advancement. Both are most easily understood as recursive algorithms.