Graph
The adjacvency-list representation of a graph consists of an array Adj , one for each vertex in . For each , the adjacency list contains all the vertices such that .
- Use space complexity
The adjacency-matrix representation of a graph consists of a matrix such that is 1 if and 0 otherwise where this such matrix is .
- Use space complexity
We always prefer the adjacency-list representation of a graph because it is more space-efficient than the adjacency-matrix representation and get more robustness in which we can augment on the node of the linked list so that we can present weight graph.
Breadth-First Search
For a graph and a start vertex , the breadth-first search of from is a traversal of the graph that visits each vertex exactly once and for each edge exactly once (may not traversal all). Such traversal graph is called a breadth-first tree. Moreover, we do color each vertex as white, gray, or black. We start with as gray and all other vertices as white. We then visit each vertex exactly once and for each edge exactly once. Once all adjacent vertices of are visited, we color as black. We do this by maintaining a queue of vertices that we have discovered but not yet visited. Initially, contains only .
The total run-time of BFS is where the initialization takes time and scanning all the edges takes time.
Some problems can be solved by BFS: shortest path, connected components, bipartite, two-colorability, etc.
- shortest path: we do BFS and return the degree of the target node. If we can successfully reach the target node, we will update the degree of the target node. Otherwise, we will see its degree is infinity which is the default value.
For a graph and a start vertex , we define predecessor subgraph of from where such that and . Such subgraph is breath-first tree if contains all reachable vertices from and there is a unique path from to in (also is the shortest path from to ).
The BFS algorithm take time.
We also define the shortest-path distance from to in as the length of the shortest path from to in .
Depth-First Search
The predecessor subgraph of a DFS froms a depth-first forest comprising several depth-first trees. We also do timestamps for each vertex which notice the arrival time and finished time. (u.d denote discovered time which start from parent time + 1, u.f denote finished time which end with the last child time + 1). According the time structure we have properties, for a node :
- if and are disjoint, then and are not a descendant of each other in the depth-first forest.
- if is a subset of , then is a descendant of in the depth-first forest.
We also define several edges of depth-first forest:
- tree edge: an edge such that .
- back edge: an edge , such that and . Self-loop is also a back edge.
- forward edge: an edge such that and .
- cross edge: all other edges.
An undirected graph , every edge of is either a tree edge or a back edge.
The DFS algorithm take time.
Topological Sort
A topological sort of a directed acyclic graph is a linear ordering of all its vertices such that if , then appears before in the ordering. We can use DFS to find a topological sort of a directed acyclic graph . Steps with precondition DAG :
- Call DFS(G) to compute finishing times for each vertex .
- as each vertex is finished, insert it onto the front of a linked list.
- return the linked list of vertices.
A directed graph is acyclic a depth-first search of never produces a back edge.
The topological sort algorithm take time.
Strongly Connected Components
A directed graph is strongly connected for every pair of vertices , there is a path from to and a path from to .
Using Edge and Transpose Edge, we can find the strongly connected components of a directed graph .
Steps:
- Call DFS(G) to compute finishing times for each vertex .
- compute .
- call DFS(G^T), but in the main loop of DFS, consider the vertices in order of decreasing (as computed in line 1).
- output the vertices of each tree in the depth-first forest formed in line 3 as a separate strongly connected component.
Component Graph: Suppose has strongly connected components. Then we define and , is the representative of . We define . Then is a directed acyclic graph.
Let and be distinct strongly connected components in directed graph . Let and , and suppose contains a path from to , then cannot also contain a path from to .
Let and be distinct strongly connected components in directed graph . Suppose edge where , . Then .