Docsity

Directed Graphs and Topological Sort - Algorithm Analysis and Design | CPSC 5115G, Study notes of Computer Science

Toggle sidebar
bg1
Directed Graphs and Topological Sort
Topological sorting is a process that is applied to directed acyclic graphs.
Definition: A graph G is a finite non-empty set of vertices, denoted V(G),
together with a (possibly empty) finite set E V(G) V(G).
If we define |V(G)| = N and |E(G)| = M, the graph G is called an (N, M)–graph.
In the formal theory, we identify each vertex by an integer.
If |V(G)| = N, we say that V(G) = {1, 2, 3, …, N}.
E(G) { (J, K) | 1 ) | 1 J N and 1 K) | 1 N }
There are a number of special classes of graph that we find interesting.
When speaking of graphs, we normally mean “simple graphs”, formally defined as
V(G) = {1, 2, 3, …, N} and E(G) { (J, K) | 1 ) | 1 J N, 1 K) | 1 N, and J K) | 1 }.
A directed graph is a simple graph in which
the edge (J, K) | 1 ) is not the same as the edge (K) | 1 , J); (J, K) | 1 ) (K) | 1 , J).
An undirected graph is a simple graph in which
the edge (J, K) | 1 ) is defined to be the same as the edge (K) | 1 , J); (J, K) | 1 ) = (K) | 1 , J).
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Directed Graphs and Topological Sort - Algorithm Analysis and Design | CPSC 5115G and more Study notes Computer Science in PDF only on Docsity!

Directed Graphs and Topological Sort

Topological sorting is a process that is applied to directed acyclic graphs. Definition: A graph G is a finite non-empty set of vertices, denoted V(G), together with a (possibly empty) finite set E  V(G)  V(G). If we define |V(G)| = N and |E(G)| = M, the graph G is called an (N, M)–graph. In the formal theory, we identify each vertex by an integer. If |V(G)| = N, we say that V(G) = {1, 2, 3, …, N}. E(G)  { (J, K) | 1 ) | 1  J  N and 1  K) | 1  N } There are a number of special classes of graph that we find interesting. When speaking of graphs, we normally mean “simple graphs” , formally defined as V(G) = {1, 2, 3, …, N} and E(G)  { (J, K) | 1 ) | 1  J  N, 1  K) | 1  N, and J  K) | 1 }. A directed graph is a simple graph in which the edge (J, K) | 1 ) is not the same as the edge (K) | 1 , J); (J, K) | 1 )  (K) | 1 , J). An undirected graph is a simple graph in which the edge (J, K) | 1 ) is defined to be the same as the edge (K) | 1 , J); (J, K) | 1 ) = (K) | 1 , J).

Graph Theory: Informal Definitions

A simple graph is a graph without edges connecting any vertex to itself; no edge of the form (J, J). The graph in the next figure is not simple, as it has edges connecting vertex 1 to itself and vertex 4 to itself. This graph at right is also described as follows V(G) = {1, 2, 3, 4} E(G) = { (1, 1), (1, 4), (2, 3), (2, 4), (4, 4) } Any simple graph over vertex set V(G) = {1, 2, 3, 4} would have E(G)  { (1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3) }

More on Directed and Undirected Graphs

Directed graphs correspond to edge sets that contain ordered pairs. Undirected graphs correspond to edge sets that contain unordered pairs. Consider the figure below, which shows an undirected graph and a directed graph. The two graphs appear similar, but are quite different. The graph at the left is said to be the undirected graph that underlies the directed graph. The undirected graph has E(G) = { (1, 4), (2, 3), (2, 4), (3,4) }; implicitly (3, 2)  E(G), (4, 1)  E(G), (4, 2)  E(G), and (4, 3)  E(G). The directed graph has E(G) = { (1, 4), (2, 4), (3, 2), (4, 1), (4, 3) }, with no edges implicitly added.

Still More on Directed and Undirected Graphs

Consider the following two graphs, one directed and one undirected. In what sense are they similar? Each graph has V(G) = {1, 2, 3, 4} The undirected graph has E(G) = { (1, 2), (1, 4), (2, 3), (3, 4) } more conventionally written as E(G) = { (1, 2), (2, 3), (3, 4), (4, 1) }. The directed graph has E(G) = { (1, 2), (2, 3), (3, 4), (4, 1), (1, 4), (4, 3), (3, 2), (2, 1) } The undirected graph can be seen as a simpler representation containing the same information as the directed graph. Nevertheless, they are not the same graph.

Topological Sorting

Topological sorting is a process applied to directed acyclic graphs. Theorem: A graph can be sorted topologically if and only if it is a directed acyclic graph. Topological sorting is an arrangement of the vertices in a D)AG in such a way that preserves the ordering implicit in the direction of the graph edges. Consider the set of course prerequisites represented by the graph below. C1 and C2 are both prerequisites for C3. C3 is a prerequisite for both C4 and C5. C4 is a prerequisite for C5.

Two Possible Topological Sorts

Here again is the sample graph. We can easily see that there are two possible topological orderings. These are C1, C2, C3, C4, C5, and C2, C1, C3, C4, C5. There are two algorithms commonly used for topological sorting.

  1. D)epth First Search
  2. Source Removal.

The Source Removal Algorithm

To sort a directed acyclic graph topologically, one repeatedly applies the following steps. Repeat Identify one source vertex Place that vertex in the sort order Remove from the D)AG that vertex and all edges incident from it. Until the Graph is Empty Give two or more candidate source vertices at any step, choose arbitrarily. The topological sort will depend on the choice taken, but all such sorts are valid. In our sample, either vertex C1 or C2 can be chosen first.

Remove the “First Source”

We start with the sample graph. We see two source vertices: C1 and C2. Arbitrarily selecting C1 to remove, we get: Our list at the moment contains one vertex (C1).

Finishing the Topological Sort

The graph now is as follows. It is seen that there is only one source vertex. Removal of that vertex gives the sort list (C1, C2, C3) and a D)AG with two vertices. The resultant D)AG has only one source vertex. Removing that vertex gives the sort list (C1, C2, C3, C4) and a D)AG consisting of the isolated vertex C5. By definition, an isolated vertex is a source vertex. Removal of that vertex gives the sort list (C1, C2, C3, C4, C5). Had we selected to start with source vertex C2, our list would have been (C2, C1, C3, C4, C5)