Directed graphs Definition A directed graph or digraph

  • Slides: 14
Download presentation
Directed graphs Definition. A directed graph (or digraph) is a pair (V, E), where

Directed graphs Definition. A directed graph (or digraph) is a pair (V, E), where V is a finite non-empty set of vertices, and E is a set of ordered pairs of distinct edges. If (v, w) E, then v is called the tail, and w is called the head of (v, w). We denote edges by v w. Example 1: Graphs for expressing a precedence relation in scheduling applications. CS 3 CS 1 CS 7 CS 4 CS 2 CS 6 CS 5

In Example 1, the precedence relation must satisfy the following conditions: 1. 2. 3.

In Example 1, the precedence relation must satisfy the following conditions: 1. 2. 3. Irreflexivity, i. e. for all s S, s -/-> s. Asymmetry, i. e. for all s, t S, if s t, then t -/-> s. Transitivity, i. e. for all s, r, t S, if r s and s t, then r t. where S is a set of vertices. Property: In a directed acyclic graph, for all s, t S, s t iff there is a path from s to t.

Example 2. Graph for expressing a binary relation. Let S = {1, 2, 3,

Example 2. Graph for expressing a binary relation. Let S = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, and R: y x iff x y and x divides y. 6 8 10 2 4 5 3 1 7 9

Depth-first search in directed graphs Same as in undirected graphs, but the result is

Depth-first search in directed graphs Same as in undirected graphs, but the result is a depth-first search forest, rather than a tree. Example:

Transitive closure of a graph The problem: Given a directed graph, G = (V,

Transitive closure of a graph The problem: Given a directed graph, G = (V, E), find all of the vertices reachable from a given starting vertex v V. Transitive closure (definition): Let G = (V, E) be a graph, where x y, y z (x, y, z V). Then we can add a new edge x z. A graph containing all of the edges of this nature is called the transitive closure of the original graph. The best way to represent the transitive closure graph (TCG) is by means of an adjacency matrix. To generate the TCG, we can use the Warshall's algorithm: for y : = 1 to Number. Of. Nodes { for x : = 1 to Number. Of. Nodes { if A[x, y] then { for z : = 1 to Number. Of. Nodes if A[y, z] then A[x, z] : = True }}} Efficiency result: Warshall's algorithm solves the transitive closure problem in O(Number. Of. Nodes^3) time. Example to be distributed in class.

The all shortest path problem The problem: Given a weighted directed graph, G =

The all shortest path problem The problem: Given a weighted directed graph, G = (V, E, W), find the shortest path between all pairs of vertices from V. Solution 1: Run Dijkstra's shortest path algorithm V times, each time with a new starting vertex. The overall efficiency of this solution will be O((E+V) Vlog. V). Solution 2: Floyd's all-pairs shortest path algorithm. Example. Distance matrix: 5 1 8 3 1 2 3 1 0 8 5 2 3 0 3 2 2

Example (contd. ) The distance matrix is computed as follows: 0, if i =

Example (contd. ) The distance matrix is computed as follows: 0, if i = j. Distance[i, j] = Edge. Weight(i, j). , otherwise. Floyd's algorithm computes a sequence of distance matrices, such that at each step k of the computation process (1 k Number. Of. Nodes), Distance[i, j] satisfies the following two criteria: 1. 2. The path starts at vertex i, and ends at vertex j. Internal vertices on the path come from the set of vertices having index values between 1 and k inclusive. In our example, the computation process is carried out as follows: Step 1: Consider all paths having node 1 as an internal node: 2 --> 1 --> 3 1 2 3 W(2, 1) = 3 1 0 8 5 W(1, 3) =5 2 3 0 8 d(2, 3) = 8 < 3 2 0 There is no path 3 --> 1 --> 2.

Example (contd. ) Step 2: Consider all paths having node 2 as an internal

Example (contd. ) Step 2: Consider all paths having node 2 as an internal node: 3 --> 2 --> 1 1 2 3 W(3, 2) = 2 1 0 8 5 W(2, 1) = 3 2 3 0 8 3 5 2 0 d(3, 1) = 5 < Second possible path 1 --> 2 --> 3 have weight 8 + 8 > 5; no change. Step 3: Consider all path having node 3 as an internal node: 1 --> 3 --> 2 1 2 3 W(1, 3) = 5 1 0 7 5 W(3, 2) = 2 2 3 0 8 3 5 2 0 d(1, 2) = 7 < 8 Second possible path 2 --> 3 --> 1 have weight 8 + 5 > 3; no change.

Floyd's algorithm (contd. ) for k : = 1 to Number. Of. Nodes {

Floyd's algorithm (contd. ) for k : = 1 to Number. Of. Nodes { for i : = 1 to Number. Of. Nodes { for j : = 1 to Number. Of. Nodes { Distance[i, j] : = min (Distance[i, j], Distance[i, k] + Distance[k, j]) }}} Efficiency result: Floyd's algorithm solves the all shortest paths problem in O(Number. Of. Nodes^3) time.

Topological ordering on directed acyclic graphs The problem: Given a directed acyclic graph (DAG),

Topological ordering on directed acyclic graphs The problem: Given a directed acyclic graph (DAG), G = (V, E), define a linear sequence in which graph nodes can be visited. Example. CS 3 CS 1 CS 7 CS 4 CS 2 CS 6 CS 5 Here such sequences are: Ø Ø CS 1 CS 2 CS 3 CS 4 CS 5 CS 6 CS 7 CS 2 CS 1 CS 5 CS 4 CS 3 CS 6 CS 7 CS 6 CS 2 CS 5 CS 1 CS 3 CS 7 and so on… These are called “topological orderings” of the graph’s nodes.

Definition. The indegree of a graph node is the number of edges entering the

Definition. The indegree of a graph node is the number of edges entering the node, and the outdegree of a graph node is the number of edges exiting from the node. Topological ordering algorithm: Compute. Initial. Indegree (G, Indegree) for n : = 1 to Number. Of. Nodes if Indegree[n] = 0 then enqueue (Zero. Q, n) order. Count : = 0 while (! Empty (Zero. Q)) { v : = dequeue(Zero. Q) order. Count : = order. Count + 1; T[order. Count] : = v for n : = 1 to Number. Of. Nodes if (edge (v, n)) then Indegree[n] : = Indegree[n] – 1 if (Indegree[n] = 0) then enqueue(Zero. Q) Efficiency result: Compute. Initial. Indegree is O(Number. Of. Nodes^2) operation. Total efficiency of topological ordering is O(Number. Of. Nodes^2).