This document discusses graph algorithms using depth-first search (DFS). It begins by defining graph terminology such as vertices, edges, directed and undirected graphs. It then explains DFS, including classifying edges based on the DFS spanning tree. It describes applications of DFS such as finding biconnected components, articulation points, and strongly connected components in directed graphs. The document provides pseudocode for DFS and algorithms for these applications that run in linear time with respect to the number of vertices and edges.
beginning with a tree T containing a single vertex
repeatedly adding the least cost edge between T and the rest of the graph
terminating when T spans all vertices
- The document discusses graphs and graph algorithms including definitions of graph terminology, representations of graphs, graph traversal algorithms like depth-first search and breadth-first search, and algorithms for finding minimum spanning trees like Kruskal's algorithm and Prim's algorithm.
- It provides detailed descriptions and pseudocode for graph traversal algorithms, algorithms to find connected components and articulation points, and minimum cost spanning tree algorithms.
- The document is intended as a curriculum for teaching graph algorithms and data structures.
A graph G is composed of a set of vertices V connected by edges E. It can be represented using an adjacency matrix, with a 1 or 0 in position (i,j) indicating whether vertices i and j are connected, or an adjacency list storing the neighbors of each vertex. Graph search algorithms like depth-first search (DFS) and breadth-first search (BFS) are used to traverse the graph and find paths between vertices, with DFS prioritizing depth and BFS prioritizing breadth of exploration. DFS uses recursion to implicitly store paths while BFS uses queues and must store paths separately.
Graph Representation, DFS and BFS Presentation.pptxbashirabdullah789
The document describes the breadth-first search (BFS) algorithm for traversing graphs. BFS uses a queue to visit all neighboring nodes at the current level before moving to the next level out. It marks nodes as discovered, processes their neighbors, and marks them finished after processing. The algorithm initializes all nodes as undiscovered, enqueues the source node, and dequeues nodes to process their neighbors until the queue is empty.
The document discusses different graph data structures including adjacency matrices and adjacency lists for representing graphs, and describes key graph terminology such as vertices, edges, paths, connected components, and directed vs undirected graphs. It also explains Dijkstra's algorithm for finding the shortest path between a starting node and all other nodes in a graph.
This document describes graphs and basic graph algorithms. It defines what a graph is as a set of vertices and edges. It also defines common graph types like directed and undirected graphs. The document then describes two common ways to represent graphs: adjacency lists and adjacency matrices. It provides pseudocode and examples for the core graph search algorithms Breadth-first Search (BFS) and Depth-first Search (DFS). BFS explores the graph in levels, starting from the source vertex. DFS explores edges deeply first before backtracking.
This document provides information about graphs and graph algorithms. It discusses different graph representations including adjacency matrices and adjacency lists. It also describes common graph traversal algorithms like depth-first search and breadth-first search. Finally, it covers minimum spanning trees and algorithms to find them, specifically mentioning Kruskal's algorithm.
This document provides an overview of graphs and graph algorithms. It begins with definitions and data structures for representing graphs. It then covers graph traversal algorithms, including depth-first search (DFS) and breadth-first search (BFS). Topological sorting of directed acyclic graphs is also discussed. Exercises are provided to implement DFS, BFS, and topological sorting.
beginning with a tree T containing a single vertex
repeatedly adding the least cost edge between T and the rest of the graph
terminating when T spans all vertices
- The document discusses graphs and graph algorithms including definitions of graph terminology, representations of graphs, graph traversal algorithms like depth-first search and breadth-first search, and algorithms for finding minimum spanning trees like Kruskal's algorithm and Prim's algorithm.
- It provides detailed descriptions and pseudocode for graph traversal algorithms, algorithms to find connected components and articulation points, and minimum cost spanning tree algorithms.
- The document is intended as a curriculum for teaching graph algorithms and data structures.
A graph G is composed of a set of vertices V connected by edges E. It can be represented using an adjacency matrix, with a 1 or 0 in position (i,j) indicating whether vertices i and j are connected, or an adjacency list storing the neighbors of each vertex. Graph search algorithms like depth-first search (DFS) and breadth-first search (BFS) are used to traverse the graph and find paths between vertices, with DFS prioritizing depth and BFS prioritizing breadth of exploration. DFS uses recursion to implicitly store paths while BFS uses queues and must store paths separately.
Graph Representation, DFS and BFS Presentation.pptxbashirabdullah789
The document describes the breadth-first search (BFS) algorithm for traversing graphs. BFS uses a queue to visit all neighboring nodes at the current level before moving to the next level out. It marks nodes as discovered, processes their neighbors, and marks them finished after processing. The algorithm initializes all nodes as undiscovered, enqueues the source node, and dequeues nodes to process their neighbors until the queue is empty.
The document discusses different graph data structures including adjacency matrices and adjacency lists for representing graphs, and describes key graph terminology such as vertices, edges, paths, connected components, and directed vs undirected graphs. It also explains Dijkstra's algorithm for finding the shortest path between a starting node and all other nodes in a graph.
This document describes graphs and basic graph algorithms. It defines what a graph is as a set of vertices and edges. It also defines common graph types like directed and undirected graphs. The document then describes two common ways to represent graphs: adjacency lists and adjacency matrices. It provides pseudocode and examples for the core graph search algorithms Breadth-first Search (BFS) and Depth-first Search (DFS). BFS explores the graph in levels, starting from the source vertex. DFS explores edges deeply first before backtracking.
This document provides information about graphs and graph algorithms. It discusses different graph representations including adjacency matrices and adjacency lists. It also describes common graph traversal algorithms like depth-first search and breadth-first search. Finally, it covers minimum spanning trees and algorithms to find them, specifically mentioning Kruskal's algorithm.
This document provides an overview of graphs and graph algorithms. It begins with definitions and data structures for representing graphs. It then covers graph traversal algorithms, including depth-first search (DFS) and breadth-first search (BFS). Topological sorting of directed acyclic graphs is also discussed. Exercises are provided to implement DFS, BFS, and topological sorting.
This document provides an overview of graphs and graph algorithms. It begins with an introduction to graphs, including definitions of vertices, edges, directed/undirected graphs, and graph representations using adjacency matrices and lists. It then covers graph traversal algorithms like depth-first search and breadth-first search. Minimum spanning trees and algorithms for finding them like Kruskal's algorithm are also discussed. The document provides examples and pseudocode for the algorithms. It analyzes the time complexity of the graph algorithms. Overall, the document provides a comprehensive introduction to fundamental graph concepts and algorithms.
The document discusses graph theory concepts including:
- Euler's analysis of the Seven Bridges of Königsberg problem which proved that a walk crossing each bridge once is impossible.
- Definitions of graphs, vertices, edges, degree of a vertex, adjacency matrix, adjacency lists, trees, forests, connectivity, graph traversal algorithms like breadth-first search and depth-first search.
- Explanations and examples are provided for graph representations and common graph terminology.
The document describes graphs and graph algorithms. It defines what a graph is composed of (vertices and edges) and different types of graphs like directed and undirected graphs. It provides terminology used with graphs like vertices, edges, paths, cycles, connectedness. It discusses ways of representing graphs through adjacency matrices and adjacency lists and provides examples. It also describes Dijkstra's algorithm, a graph algorithm to find the shortest path between vertices in a graph.
This document discusses several graph algorithms:
1) Topological sort is an ordering of the vertices of a directed acyclic graph (DAG) such that for every edge from vertex u to v, u comes before v in the ordering. It can be used to find a valid schedule respecting dependencies.
2) Strongly connected components are maximal subsets of vertices in a directed graph such that there is a path between every pair of vertices. An algorithm uses depth-first search to find SCCs in linear time.
3) Minimum spanning trees find a subset of edges that connects all vertices at minimum total cost. Prim's and Kruskal's algorithms find minimum spanning trees using greedy strategies in O(E
This document discusses several graph algorithms:
1) Topological sort is an ordering of the vertices of a directed acyclic graph (DAG) such that for every edge from vertex u to v, u comes before v in the ordering. It can be used to find a valid schedule respecting dependencies.
2) Strongly connected components are maximal subsets of vertices in a directed graph such that there is a path between every pair of vertices. An algorithm uses depth-first search to find SCCs in linear time.
3) Minimum spanning trees find a subset of edges that connects all vertices at minimum total cost. Prim's and Kruskal's algorithms find minimum spanning trees using greedy strategies in O(E
The document discusses different representations of graphs and algorithms for traversing graphs. It describes adjacency matrices and adjacency lists for representing graphs, with adjacency matrices using O(V^2) space and lists using O(V+E) space. It then explains depth-first search (DFS) and breadth-first search (BFS) traversal algorithms, with DFS implemented recursively using a stack and BFS using a queue to visit nodes level-by-level. The relation between DFS and BFS is that they both maintain an edge list but DFS removes from the end like a stack while BFS removes from the beginning like a queue.
This document defines key graph terminology and concepts. It begins by defining what a graph is composed of - vertices and edges. It then discusses directed vs undirected graphs and defines common graph terms like adjacent vertices, paths, cycles, and more. The document also covers different ways to represent graphs, such as adjacency matrices and adjacency lists. Finally, it briefly introduces common graph search methods like breadth-first search and depth-first search.
1. The document defines various graph terms including vertices, edges, paths, cycles, connectivity, and graph representations.
2. It discusses topological sorting of directed acyclic graphs (DAGs) which finds an ordering of vertices such that if there is a path from v_i to v_j, then v_j appears after v_i. This can be done in O(|V|+|E|) time.
3. It also covers shortest path algorithms, including ones for unweighted graphs (BFS, O(|V|+|E|)), weighted graphs without negative edges (Dijkstra's algorithm, O(|E|log|V|)), and weighted graphs allowing negative
This document discusses graph algorithms and graph search techniques. It begins with an introduction to graphs and their representations as adjacency matrices and adjacency lists. It then covers graph terminology like vertices, edges, paths, cycles, and weighted graphs. Common graph search algorithms like breadth-first search and depth-first search are explained. Variations of these algorithms like recursive depth-first search and Dijkstra's algorithm for finding shortest paths in weighted graphs are also covered. Examples are provided throughout to illustrate the concepts and algorithms.
Graph theory is the study of graphs, which are mathematical structures used to model pairwise relations between objects. A graph consists of vertices and edges connecting pairs of vertices. There are many types of graphs including trees, which are connected acyclic graphs. Spanning trees are subgraphs of a graph that connect all vertices using the minimum number of edges. Key concepts in graph theory include paths, connectedness, cycles, and isomorphism between graphs.
The document discusses using graphs to model real-world problems and algorithms for solving graph problems. It introduces basic graph terminology like vertices, edges, adjacency matrix/list representations. It then summarizes algorithms for finding shortest paths like Dijkstra's and Bellman-Ford. It also discusses using depth-first search to solve problems like detecting cycles, topological sorting of vertices, and calculating longest paths in directed acyclic graphs.
This document discusses graphs and graph data structures. It defines a graph as a pictorial representation of a set of objects connected by links, with the objects represented as vertices and the links as edges. It provides definitions and examples of basic graph terminology like vertices, edges, adjacency, and different types of graphs like directed vs undirected graphs. It also covers graph implementations using adjacency matrices and adjacency lists, as well as common graph algorithms like depth-first search and breadth-first search. Finally, it lists some applications of graphs like social networks, maps, and computer networks.
This document provides an overview of graph theory concepts. It defines what a graph is consisting of vertices and edges. It describes different types of graphs such as directed vs undirected, simple vs complex graphs. It introduces common graph terminology like degree of a vertex, adjacent/incident vertices, and connectivity. Examples of applications are given such as transportation networks, web graphs, and scheduling problems. Special graph cases like complete graphs and cycles are also defined.
Graphs can be represented abstractly as a set of vertices and edges connecting pairs of vertices. They are used to model various real-world relationships and can be represented using adjacency lists or matrices. Breadth-first search (BFS) and depth-first search (DFS) are common graph traversal algorithms that systematically explore the edges of a graph to discover reachable vertices. BFS produces a breadth-first tree and finds the shortest paths from the source, while DFS explores edges deeper first before backtracking.
CS-102 Data Structure lectures on Graphsssuser034ce1
The document defines and explains various graph concepts:
- It describes graph representations like adjacency matrices and lists, and types of graphs like undirected, directed, and weighted.
- Key graph terminology is introduced such as vertices, edges, paths, cycles, connectedness, subgraphs, and degrees of vertices.
- Examples are provided to illustrate concepts like complete graphs, trees, and bipartite graphs.
- Graph representations like adjacency matrices and linked/packed adjacency lists are also summarized.
CS-102 Data Structure lectures on Graphsssuser034ce1
The document defines and explains various graph concepts:
- It describes graph representations like adjacency matrices and lists, and types of graphs like undirected, directed, and weighted.
- Key graph terminology is introduced such as vertices, edges, paths, cycles, connectedness, subgraphs, and degrees of vertices.
- Examples are provided to illustrate concepts like complete graphs, trees, and bipartite graphs.
- Graph representations like adjacency matrices and linked/packed adjacency lists are also summarized.
The document discusses graphs and graph algorithms. It defines graphs and their representations using adjacency lists and matrices. It then describes the Breadth-First Search (BFS) and Depth-First Search (DFS) algorithms for traversing graphs. BFS uses a queue to explore the neighbors of each vertex level-by-level, while DFS uses a stack to explore as deep as possible first before backtracking. Pseudocode and examples are provided to illustrate how the algorithms work.
This document discusses various graph algorithms including depth-first search (DFS), breadth-first search (BFS), union find, Kruskal's algorithm, Floyd-Warshall's algorithm, Dijkstra's algorithm, and bipartite graphs. It provides definitions, pseudocode, sample code, and sample problems for implementing each algorithm.
graphin-c1.png
graphin-c1.txt
1: 2
2: 3 8
3: 4
4: 5
5: 3
6: 7
7: 3 6 8
8: 1 9
9: 1
graphin-c2.jpg
graphin-c2.txt
1: 2 9
2: 3 8
3: 4
4: 5 9
5: 3
6: 7
7: 3 6 8
8: 1
9:
graphin-DAG.png
graphin-DAG.txt
1: 2
2: 3 8
3: 4
4: 5
5: 9
6: 4 7
7: 3 8
8: 9
9:
CS 340 Programming Assignment III:
Topological Sort
Description: You are to implement the Depth-First Search (DFS) based algorithm for (i)
testing whether or not the input directed graph G is acyclic (a DAG), and (ii) if G is a DAG,
topologically sorting the vertices of G and outputting the topologically sorted order.
I/O Specifications: You will prompt the user from the console to select an input graph
filename, including the sample file graphin.txt as an option. The graph input files must be of
the following adjacency list representation where each xij is the j'th neighbor of vertex i (vertex
labels are 1 through n):
1: x11 x12 x13 ...
2: x21 x22 x23 ...
.
.
n: xn1 xn2 xn3 ...
Your output will be to the console. You will first output whether or not the graph is acyclic. If
the graph is NOT acyclic, then you will output the set of back edges you have detected during
DFS. Otherwise, if the graph is acyclic, then you will output the vertices in topologically
sorted order.
Algorithmic specifications:
Your algorithm must use DFS appropriately and run in O(E + V) time on any input graph. You will
need to keep track of edge types and finish times so that you can use DFS for detecting
cyclicity/acyclicity and topologically sorting if the graph is a DAG. You may implement your graph
class as you wish so long as your overall algorithm runs correctly and efficiently.
What to Turn in: You must turn in a single zipped file containing your source code, a Makefile
if your language must be compiled, appropriate input and output files, and a README file
indicating how to execute your program (especially if not written in C++ or Java). Refer to
proglag.pdf for further specifications.
This assignment is due by MIDNIGHT of Monday, February 19. Late submissions
carry a minus 40% per-day late penalty.
Sheet1Name:Possible:Score:Comments:10Graph structure with adjacency list representationDFS16Correct and O(V+E) time10Detecting cycles, is graph DAG?Topological Sort16Correctness of Topo-Sort algorithm and output18No problems in compilation and execution? Non-compiling projects receive max total 10 points, and code that compiles but crashes during execution receives max total 18 points.700Total
&"Helvetica,Regular"&12&K000000&P
Sheet2
&"Helvetica,Regular"&12&K000000&P
Sheet3
&"Helvetica,Regular"&12&K000000&P
DFS and topological sort
CS340
Depth first search
breadth
depth
Search "deeper" whenever possible
*example shows discovery times
Depth first search
Input: G = (V,E), directed or undirected.
No source vertex is given!
Output: 2 timestamps on each vertex:
v.d discovery time
v.f finishing time
These will be useful ...
This document provides an overview of graphs and graph algorithms. It begins with an introduction to graphs, including definitions of vertices, edges, directed/undirected graphs, and graph representations using adjacency matrices and lists. It then covers graph traversal algorithms like depth-first search and breadth-first search. Minimum spanning trees and algorithms for finding them like Kruskal's algorithm are also discussed. The document provides examples and pseudocode for the algorithms. It analyzes the time complexity of the graph algorithms. Overall, the document provides a comprehensive introduction to fundamental graph concepts and algorithms.
The document discusses graph theory concepts including:
- Euler's analysis of the Seven Bridges of Königsberg problem which proved that a walk crossing each bridge once is impossible.
- Definitions of graphs, vertices, edges, degree of a vertex, adjacency matrix, adjacency lists, trees, forests, connectivity, graph traversal algorithms like breadth-first search and depth-first search.
- Explanations and examples are provided for graph representations and common graph terminology.
The document describes graphs and graph algorithms. It defines what a graph is composed of (vertices and edges) and different types of graphs like directed and undirected graphs. It provides terminology used with graphs like vertices, edges, paths, cycles, connectedness. It discusses ways of representing graphs through adjacency matrices and adjacency lists and provides examples. It also describes Dijkstra's algorithm, a graph algorithm to find the shortest path between vertices in a graph.
This document discusses several graph algorithms:
1) Topological sort is an ordering of the vertices of a directed acyclic graph (DAG) such that for every edge from vertex u to v, u comes before v in the ordering. It can be used to find a valid schedule respecting dependencies.
2) Strongly connected components are maximal subsets of vertices in a directed graph such that there is a path between every pair of vertices. An algorithm uses depth-first search to find SCCs in linear time.
3) Minimum spanning trees find a subset of edges that connects all vertices at minimum total cost. Prim's and Kruskal's algorithms find minimum spanning trees using greedy strategies in O(E
This document discusses several graph algorithms:
1) Topological sort is an ordering of the vertices of a directed acyclic graph (DAG) such that for every edge from vertex u to v, u comes before v in the ordering. It can be used to find a valid schedule respecting dependencies.
2) Strongly connected components are maximal subsets of vertices in a directed graph such that there is a path between every pair of vertices. An algorithm uses depth-first search to find SCCs in linear time.
3) Minimum spanning trees find a subset of edges that connects all vertices at minimum total cost. Prim's and Kruskal's algorithms find minimum spanning trees using greedy strategies in O(E
The document discusses different representations of graphs and algorithms for traversing graphs. It describes adjacency matrices and adjacency lists for representing graphs, with adjacency matrices using O(V^2) space and lists using O(V+E) space. It then explains depth-first search (DFS) and breadth-first search (BFS) traversal algorithms, with DFS implemented recursively using a stack and BFS using a queue to visit nodes level-by-level. The relation between DFS and BFS is that they both maintain an edge list but DFS removes from the end like a stack while BFS removes from the beginning like a queue.
This document defines key graph terminology and concepts. It begins by defining what a graph is composed of - vertices and edges. It then discusses directed vs undirected graphs and defines common graph terms like adjacent vertices, paths, cycles, and more. The document also covers different ways to represent graphs, such as adjacency matrices and adjacency lists. Finally, it briefly introduces common graph search methods like breadth-first search and depth-first search.
1. The document defines various graph terms including vertices, edges, paths, cycles, connectivity, and graph representations.
2. It discusses topological sorting of directed acyclic graphs (DAGs) which finds an ordering of vertices such that if there is a path from v_i to v_j, then v_j appears after v_i. This can be done in O(|V|+|E|) time.
3. It also covers shortest path algorithms, including ones for unweighted graphs (BFS, O(|V|+|E|)), weighted graphs without negative edges (Dijkstra's algorithm, O(|E|log|V|)), and weighted graphs allowing negative
This document discusses graph algorithms and graph search techniques. It begins with an introduction to graphs and their representations as adjacency matrices and adjacency lists. It then covers graph terminology like vertices, edges, paths, cycles, and weighted graphs. Common graph search algorithms like breadth-first search and depth-first search are explained. Variations of these algorithms like recursive depth-first search and Dijkstra's algorithm for finding shortest paths in weighted graphs are also covered. Examples are provided throughout to illustrate the concepts and algorithms.
Graph theory is the study of graphs, which are mathematical structures used to model pairwise relations between objects. A graph consists of vertices and edges connecting pairs of vertices. There are many types of graphs including trees, which are connected acyclic graphs. Spanning trees are subgraphs of a graph that connect all vertices using the minimum number of edges. Key concepts in graph theory include paths, connectedness, cycles, and isomorphism between graphs.
The document discusses using graphs to model real-world problems and algorithms for solving graph problems. It introduces basic graph terminology like vertices, edges, adjacency matrix/list representations. It then summarizes algorithms for finding shortest paths like Dijkstra's and Bellman-Ford. It also discusses using depth-first search to solve problems like detecting cycles, topological sorting of vertices, and calculating longest paths in directed acyclic graphs.
This document discusses graphs and graph data structures. It defines a graph as a pictorial representation of a set of objects connected by links, with the objects represented as vertices and the links as edges. It provides definitions and examples of basic graph terminology like vertices, edges, adjacency, and different types of graphs like directed vs undirected graphs. It also covers graph implementations using adjacency matrices and adjacency lists, as well as common graph algorithms like depth-first search and breadth-first search. Finally, it lists some applications of graphs like social networks, maps, and computer networks.
This document provides an overview of graph theory concepts. It defines what a graph is consisting of vertices and edges. It describes different types of graphs such as directed vs undirected, simple vs complex graphs. It introduces common graph terminology like degree of a vertex, adjacent/incident vertices, and connectivity. Examples of applications are given such as transportation networks, web graphs, and scheduling problems. Special graph cases like complete graphs and cycles are also defined.
Graphs can be represented abstractly as a set of vertices and edges connecting pairs of vertices. They are used to model various real-world relationships and can be represented using adjacency lists or matrices. Breadth-first search (BFS) and depth-first search (DFS) are common graph traversal algorithms that systematically explore the edges of a graph to discover reachable vertices. BFS produces a breadth-first tree and finds the shortest paths from the source, while DFS explores edges deeper first before backtracking.
CS-102 Data Structure lectures on Graphsssuser034ce1
The document defines and explains various graph concepts:
- It describes graph representations like adjacency matrices and lists, and types of graphs like undirected, directed, and weighted.
- Key graph terminology is introduced such as vertices, edges, paths, cycles, connectedness, subgraphs, and degrees of vertices.
- Examples are provided to illustrate concepts like complete graphs, trees, and bipartite graphs.
- Graph representations like adjacency matrices and linked/packed adjacency lists are also summarized.
CS-102 Data Structure lectures on Graphsssuser034ce1
The document defines and explains various graph concepts:
- It describes graph representations like adjacency matrices and lists, and types of graphs like undirected, directed, and weighted.
- Key graph terminology is introduced such as vertices, edges, paths, cycles, connectedness, subgraphs, and degrees of vertices.
- Examples are provided to illustrate concepts like complete graphs, trees, and bipartite graphs.
- Graph representations like adjacency matrices and linked/packed adjacency lists are also summarized.
The document discusses graphs and graph algorithms. It defines graphs and their representations using adjacency lists and matrices. It then describes the Breadth-First Search (BFS) and Depth-First Search (DFS) algorithms for traversing graphs. BFS uses a queue to explore the neighbors of each vertex level-by-level, while DFS uses a stack to explore as deep as possible first before backtracking. Pseudocode and examples are provided to illustrate how the algorithms work.
This document discusses various graph algorithms including depth-first search (DFS), breadth-first search (BFS), union find, Kruskal's algorithm, Floyd-Warshall's algorithm, Dijkstra's algorithm, and bipartite graphs. It provides definitions, pseudocode, sample code, and sample problems for implementing each algorithm.
graphin-c1.png
graphin-c1.txt
1: 2
2: 3 8
3: 4
4: 5
5: 3
6: 7
7: 3 6 8
8: 1 9
9: 1
graphin-c2.jpg
graphin-c2.txt
1: 2 9
2: 3 8
3: 4
4: 5 9
5: 3
6: 7
7: 3 6 8
8: 1
9:
graphin-DAG.png
graphin-DAG.txt
1: 2
2: 3 8
3: 4
4: 5
5: 9
6: 4 7
7: 3 8
8: 9
9:
CS 340 Programming Assignment III:
Topological Sort
Description: You are to implement the Depth-First Search (DFS) based algorithm for (i)
testing whether or not the input directed graph G is acyclic (a DAG), and (ii) if G is a DAG,
topologically sorting the vertices of G and outputting the topologically sorted order.
I/O Specifications: You will prompt the user from the console to select an input graph
filename, including the sample file graphin.txt as an option. The graph input files must be of
the following adjacency list representation where each xij is the j'th neighbor of vertex i (vertex
labels are 1 through n):
1: x11 x12 x13 ...
2: x21 x22 x23 ...
.
.
n: xn1 xn2 xn3 ...
Your output will be to the console. You will first output whether or not the graph is acyclic. If
the graph is NOT acyclic, then you will output the set of back edges you have detected during
DFS. Otherwise, if the graph is acyclic, then you will output the vertices in topologically
sorted order.
Algorithmic specifications:
Your algorithm must use DFS appropriately and run in O(E + V) time on any input graph. You will
need to keep track of edge types and finish times so that you can use DFS for detecting
cyclicity/acyclicity and topologically sorting if the graph is a DAG. You may implement your graph
class as you wish so long as your overall algorithm runs correctly and efficiently.
What to Turn in: You must turn in a single zipped file containing your source code, a Makefile
if your language must be compiled, appropriate input and output files, and a README file
indicating how to execute your program (especially if not written in C++ or Java). Refer to
proglag.pdf for further specifications.
This assignment is due by MIDNIGHT of Monday, February 19. Late submissions
carry a minus 40% per-day late penalty.
Sheet1Name:Possible:Score:Comments:10Graph structure with adjacency list representationDFS16Correct and O(V+E) time10Detecting cycles, is graph DAG?Topological Sort16Correctness of Topo-Sort algorithm and output18No problems in compilation and execution? Non-compiling projects receive max total 10 points, and code that compiles but crashes during execution receives max total 18 points.700Total
&"Helvetica,Regular"&12&K000000&P
Sheet2
&"Helvetica,Regular"&12&K000000&P
Sheet3
&"Helvetica,Regular"&12&K000000&P
DFS and topological sort
CS340
Depth first search
breadth
depth
Search "deeper" whenever possible
*example shows discovery times
Depth first search
Input: G = (V,E), directed or undirected.
No source vertex is given!
Output: 2 timestamps on each vertex:
v.d discovery time
v.f finishing time
These will be useful ...
Ready to Unlock the Power of Blockchain!Toptal Tech
Imagine a world where data flows freely, yet remains secure. A world where trust is built into the fabric of every transaction. This is the promise of blockchain, a revolutionary technology poised to reshape our digital landscape.
Toptal Tech is at the forefront of this innovation, connecting you with the brightest minds in blockchain development. Together, we can unlock the potential of this transformative technology, building a future of transparency, security, and endless possibilities.
HijackLoader Evolution: Interactive Process HollowingDonato Onofri
CrowdStrike researchers have identified a HijackLoader (aka IDAT Loader) sample that employs sophisticated evasion techniques to enhance the complexity of the threat. HijackLoader, an increasingly popular tool among adversaries for deploying additional payloads and tooling, continues to evolve as its developers experiment and enhance its capabilities.
In their analysis of a recent HijackLoader sample, CrowdStrike researchers discovered new techniques designed to increase the defense evasion capabilities of the loader. The malware developer used a standard process hollowing technique coupled with an additional trigger that was activated by the parent process writing to a pipe. This new approach, called "Interactive Process Hollowing", has the potential to make defense evasion stealthier.
Discover the benefits of outsourcing SEO to Indiadavidjhones387
"Discover the benefits of outsourcing SEO to India! From cost-effective services and expert professionals to round-the-clock work advantages, learn how your business can achieve digital success with Indian SEO solutions.
1. Graph Algorithms Using Depth
First Search
Prepared by
John Reif, Ph.D.
Distinguished Professor of Computer Science
Duke University
2. Graph Algorithms Using Depth First
Search
a) Graph Definitions
b) DFS of Graphs
c) Biconnected Components
d) DFS of Digraphs
e) Strongly Connected Components
3. Readings on Graph Algorithms
Using Depth First Search
• Reading Selection:
– CLR, Chapter 22
5. Directed and Undirected Graphs
• G directed
• G undirected
if edges ordered pairs (u,v)
· ·
u v
if edges unordered pairs {u,v}
· ·
u v
6. Proper Graphs and Subgraphs
• Proper graph:
– No loops
– No multi-edges
• Subgraph G’ of G
G’ = (V’, E’) where
V’ is a subset of V and E’ is a subset of E
between vertices of V’.
7. Paths in Graphs
• Path p
P is a sequence of vertices v0, v1, …, vk
where for i=1,…k, vi-1 is adjacent to vi
Equivalently, p is a sequence of edges
e1, …, ek where for i = 2,…k each
consecutive pair of edges ei-1, ei share a
vertex
V1
V2 Vk-1 Vk
ek
e2
e1
Vo
8. Simple Paths and Cycles
• Simple path
no edge or vertex repeated,
except possibly vo = vk
• Cycle
a path p with vo = vk where k > 1
V1
V2 Vk-1
Vk = Vo
10. Connected Components of an
Undirected Graph
else G has 2 :
maximal connected subgraphs
connected components
³
11. Biconnected Component:
sets of edges where:
or is a single edge
There are a least two disjoint paths
between each pair of vertices.
12. Size of a Graph
• Graph G = (V,E)
n = |V| = # vertices
m = |E| = # edges
size of G is n+m
4
2 3
1
13. Representing a Graph
by an Adjacency Matrix
2
A is size n n
1 2 3 4
1 0 1 1 0
1 (i,j) E 2 1 0 1 0
A(i,j)
0 else 3 1 1 0 1
4 0 0 1 0
space cost n -n
´
Î
ì
= í
î
4
2 3
1
Adjacency Matrix A
14. Adjacency List Representation
of a Graph
• Adjacency Lists Adj (1),…,Adj (n)
Adj(v) = list of vertices adjacent to v
space cost O(n+m)
1
2
3
4
2
3
1
1
3
3
2 4
4
2 3
1
15. Definition of an Undirected Tree
• Tree
T is graph with unique simple path
between every pair of vertices
n = # vertices
n-1 = # edges
• Forest
– set of trees
16. Definition of a Directed Tree
• Directed Tree
T is digraph with distinguished vertex root r
such that each vertex reachable from r by a
unique path
Family Relationships:
- ancestors
- descendants
- parent
- child
- siblings
r
leaves have no proper descendants
17. An Ordered Tree
• Ordered Tree
– is a directed tree with siblings ordered
B
A
C
D
E
F
H I
18. Preorder Tree Traversal
• Preorder: A,B,C,D,E,F,H,I
[1] root (order vertices as pushed on
stack)
[2] preorder left subtree
[3] preorder right subtree
B
A
C
D
E
F
H I
19. Postorder Tree Traversal
• Postorder: B,E,D,H,I,F,C,A
[1] postorder left subtree
[2] postorder right subtree
[3] root (order vertices as popped off
stack)
B
A
C
D
E
F
H I
20. Spanning Tree and Forest of a
Graph
• T is a spanning tree of graph G if
(1) T is a directed tree with the same vertex
set as G
(2) each edge of T is a directed version of an edge of
G
• Spanning Forest:
forest of spanning trees of connected components of
G
21. Example Spanning Tree of a Graph
1
6
2
4
3
5
7
8
root
tree edge back edge
9
10
11
12
No cross edge
22. Classification of Edges of G with
Spanning Tree T
• An edge (u,v) of T is tree edge
• An edge (u,v) of G-T is back edge if u is a descendent
or ancestor of v.
• Else (u,v) is a cross edge (do not exist in undirected
DFS)
23. Tarjan’s Depth First Search Algorithm
• We assume a Random Access Machine (RAM)
computational model
• Algorithm Depth First Search
graph G (V,E) represented by
adjacency lists Adj(v) for each v V
[0] N 0
[1] all v V (number (v) 0
children (v) ( ) )
[2] all v V do
Input
for do
od
for
=
Î
¬
Î ¬
¬
Î
number (v) 0 DFS(v)
[3] spanning forest defined by children
if then
output
=
24. Recursive DFS Procedure
DFS(v)
[1] N N + 1; number (v) N
[2] for each u Adj(v)
number (u) 0
(add u to children (v); DFS(u))
recursive procedure
do
if then
¬ ¬
Î
=
The preorder numbers give the order the vertices are first
visited in DFS.
25. Time Cost of Depth First Search
(DFS) Algorithm on a RAM
• Input size n = |V|, m = |E|
• Theorem Depth First Search takes linear time cost
O(n+m)
• Proof
Can associate with each edge and vertex
a constant number of operations.
26. Classification of Edges of G via DFS
Spanning Tree T
• Edge notation induced by
• Depth First Search Tree T
u v iff (u,v) is tree edge of T
u v iff u is an ancestor of v
u --- v iff (u,v) is backedge if (u,v) G-T
with either u v or v u
27. Classification of Edges of Graph G
via DFS Spanning Tree T
1
6
2
4
3
5
7
8
G
1
6
2
4
3
5
7
8
T
Preorder numbering vertices by
order visited in DFS
DFS Spanning Tree T:
28. Classification of Edges of G via DFS
Spanning Tree T (cont’d)
• Note DFS tree T of an undirected graph has no
cross edges (u,v) where u,v are unrelated in T
u u
29. Verifying Vertices are Descendants
via Preordering of Tree
• Suppose we preorder number a Tree T
Let Dv = # of descendants of v (found in DFS)
• Lemma
u is descendant of v
iff v < u < v + Dv
v
u
Dv
W
*
30. Testing for Proper Ancestors via
Preordering
• Lemma
If u is descendant of v
and (u,w) is back edge s.t. w < v
then w is a proper ancestor of v
31. Low Values
• For each vertex v,
define low(v) = min ( {v} ∪ {w | v→ - - - w} )
• Can prove by induction:
Lemma
Can be computed during DFS in postorder.
*
low(v) min ( {v} {low(w)|v w} {w|v - - - w} )
= È ® È ®
32. Graph with DFS Numbering of
vertices [Low Values in Brackets]
7[5]
1[1]
2[2]
4[2]
3[2]
5[1]
8[1]
6[5]
v[low(v)]
33. Biconnected Components
• G is Biconnected iff either
(1) G is a single edge, or
(2) for each triple of vertices u,v,w
w-avoiding path from u to v
(equivalently: two disjoint paths from u to v)
$
$
34. Example of Biconnected
Components of Graph G
• Maximal edge subgraphs
of G which are biconnected.
5
1
8
1
6
2
4
3
5
7
8
1
2
2
3
4
6
5
7
Biconnected
Components
G
35. Biconnected Components Meet at
Articulation Points
• The intersection of two biconnected components
consists of at most one vertex called an Articulation
Point.
• Example: 1,2,5 are articulation points
1
6
2
4
3
5
7
8
G
36. Discovery of Biconnected
Components via Articulation Points
=> If can find articulation points
then can compute biconnected components:
Algorithm:
• During DFS, use auxiliary stack to store visited
edges.
• Each time we complete the DFS of a tree child of an
articulation point, pop all stacked edges
• currently in stack
• These popped off edges form a biconnected
component
37. Characterization of an Articulation Point
• Theorem
a is an articulation point iff either
(1) a is root with ≥ 2 tree children
or
(2) a is not root but a has a tree child v with
low (v) ≥ a
(note easy to check given low computation)
38. Proof of Characterization of an
Articulation Point
• Proof
The conditions are sufficient since any
a-avoiding path from v remains in the
subtree Tv rooted at v, if v is a child of a
• To show condition necessary, assume a is
an articulation point.
39. Characterization of an Articulation
Point (cont’d)
• Case (1)
If a is a root and is articulation point,
a must have ≥ 2 tree edges to two distinct biconnected
components.
• Case(2)
If a is not root, consider graph G - {a}
which must have a connected component C consisting
of only descendants of a, and
with no backedge from C to an ancestor
of v. Hence a has a tree child v in C and
low (v) ≥ a
40. Proof of Characterization of an
Articulation Point (cont’d)
a
v
root
Articulation Point a
is not the root
subtree Tv
C
no
back
edges
• Case (2)
41. Computing Biconnected Components
• Theorem
The Biconnected Components of G = (V,E) can
be computed in time O(|V|+|E|) using a RAM
• Proof idea
• Use characterization of Bicoconnected
components via articulation points
• Identify these articulation points dynamically
during depth first search
• Use a secondary stack to store the edges of the
biconnected components as they are visited
• When an articulation point is discovered , pop the
edges of this stack off to output a biconnected
component
42. Biconnected Components Algorithm
[0] initialize a STACK to empty
During a DFS traversal do
[1] add visited edge to STACK
[2] compute low of visited vertex v using Lemma
[3] test if v is an articulation point
[4] if so, for each u ∈ children (v) in order
where low (u) > v
do Pop all edges in STACK
upto and including tree edge (v,u)
Output popped edges as a
biconnected component of G
od
43. Time Bounds of Biconnected
Components Algorithm
• Time Bounds:
Each edge and vertex can be associated
with 0(1) operations. So time O(|V|+|E|).
44. Depth First Search in a Directed Graph
Depth first search tree T of Directed Graph
cross cross
cross
cycle
forward
cycle
1
1
2
3
4
5
6
7
8
2
4
5
6
7
8
G = (V,E)
i = DFS number = order
discovered
v
j = postorder = order vertex
popped off DFS stack
edge set E partitioned:
3
forward
The preorder numbers give order
vertices are first visited in DFS:
The postorder numbers give order
vertices are last visited in DFS.
45. Classification of Directed Edge (u, v)
via DFS Spanning Tree T
• Tree edge (u,v) : (u parent of v in T)
• Cycle edge (u,v): (u is descendant of v in T)
• Forward edge (u,v): (u is descendant of v in T)
• Cross edge(u,v): (u,v not related in T)
*
if v u
if u v in T
®
otherwise
*
if (u,v) T
but u v in T
46. Topological Order of Acyclic
Directed Graph
• Digraph G = (V,E) is acyclic if it has
no cycles
1 n
i j
V {v ,...v } satisfies
(v ,v ) E i j
Topological Order
=
Î Þ <
47. Characterization of an Acyclic Digraph
• Proof
Case (1):
• Suppose (u,v) ∈ E is a cycle edge, so v → u.
• But let e1,…ek be the tree edges from v to u.
• Then (u,v), e1,…ek is a cycle.
*
G is acylic iff no cycle edge
$
48. Characterization of an Acyclic
Digraph (cont’d)
Case (2):
• Suppose G has no a cycle edge
• Then order vertices in postorder of DFS
spanning forest (i.e. in order vertices are
popped off DFS stack).
• This is a reverse topological order of G.
• So, G can have no cycles.
Note:
Gives an O(|V|+|E|) algorithm
for computing Topological Ordering
of an acyclic graph G = (V,E)
49. Strong Components of Directed
Graph
• Strong Component
• Collapsed Graph
G* derived by
collapsing each strong component
into a single vertex.
note: G* is acyclic.
maximum set vertices
S of V such that u,v S
cycle containing u,v
50. Directed Graph with Strong
Components Circled
Directed Graph G = (V,E)
3
1
2
4
7
5
6
8
51. Algorithm Strong Components
Input digraph G
[1] Perform DFS on G. Renumber vertices
by postorder of the DFS of G.
[2] Let G- be the reverse digraph derived
from G by reversing direction of each edge.
52. Algorithm Strong Components
(cont’d)
[3] Perform DFS on G-, starting at highest
(in postorder of G) numbered vertex.
Output resulting DFS tree of G- as a strongly
connected component of G.
[4] repeat [3], starting at highest numbered
vertex not so for visited (halt when all
vertices visited)
53. Time Bounds of Algorithm Strong
Components
• Time Bounds
each DFS costs time c(|V|+|E|)
So total time = 2*c(|V|+|E|)
≤ O(|V|+|E|)
54. Example of Step [1] of Strong
Components Algorithm
3
1
2
4
7
5
6
8
8
7
6
5
3
4
2
1
Example Input
Digraph G
(postorder numbering
of G given in gold)
55. Example of Step [2] of Strong
Components Algorithm
3
1
2
4
7
5
6
8
8
7
6
5
3
4
2
1
Reverse
Digraph G-
(postorder numbering
of G given in gold)
56. Proof of Strong Components Algorithm
• Theorem
The Algorithm outputs the
strong components of G.
• Proof
We must show these are exactly the vertices in each
DFS spanning forest of reverse graph G-
57. Proof of Strong Components
Algorithm (continued)
• Suppose:
• v,w in the same strong component and DFS search
in G- starts at vertex r and reaches v.
• Then w will also be reached in the DFS search in G- .
• So v,w are output together in the same spanning tree of
G-.
58. Proof of Strong Components
Algorithm (continued)
• Suppose:
• v,w output in same spanning tree of G-.
• Let r be the root of that spanning tree of G-.
• Then there exists paths in G- from r to each of v and w.
• So there exists paths in G to r from each of v and w.
59. Proof of Strong Components
Algorithm (continued)
• r is root of spanning tree of G- containing v and w.
• Suppose: no path in G to r from v.
• Then since r has a higher postorder than v, there is no
path in G from v to r, a contradiction.
• Hence, there exists path in G from r to v, and similar
argument gives path from r to w.
• So v and w are in a cycle of G and must be in the same
strong component, completing proof !