1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of...

24
1 Chapter 9 Graph Algorithms • Real-life graph problems • Algorithms for some graph problems • Choice of data structures for graph problems

Transcript of 1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of...

1

Chapter 9 Graph Algorithms

• Real-life graph problems

• Algorithms for some graph problems

• Choice of data structures for graph problems

2

9.1 Definition

• A graph G = (V, E) consists of a set of vertices, V, and a set of edges, E.

• Each edge (arc) is a pair (v, w), where v,w V. An edge may have a weight (cost).

• If the pair is ordered, then the graph is directed - digraph.

• Vertex w is adjacent to v if and only if (v, w) E.

3

9.1 Definition• A path is a sequence of vertices w1, w2, w3, ...,

wN, such that (wi, wi+1) E for 1i<N.

• The length of a path is the number of edges on the path, which is N-1.

• A simple path is one such that all vertices are distinct, except that the first and the last could be the same.

• A cycle in a directed graph is a path of length at least 1 such that w1 = wN.

4

9.1 Definition• A directed graph is acyclic if it has no

cycles (abbreviated as DAG).• The indegree of a vertex v is the number of

edges (u, v).• An undirected graph is connected if there is

a path from every vertex to every other vertex.– A directed graph with this property is called

strongly directed.

5

9.1 Definition– If a directed graph is not strongly connected, but

the underlying graph (without direction to the arcs) is connected, then the graph is said to be weakly connected.

– A complete graph is a graph in which there is an edge between every pair of vertices.

• Examples of graph models include computer networks, job scheduling.

• For vertices with names, use hash table to map names to numbers.

6

9.1 Definition

1 2

3 4 5

6 7

Figure 9.1 A directed graph

7

9.1 Definition• Matrix representation

– 2-dimensional array, A, adjacency matrix.

– For each edge (u, v), set A [u] [v] = 1; otherwise the entry is 0.

– If the edge has a weight associated with it, set A [u] [v] to the weight.

– Space requirement is (|V|2); alright if the graph is dense, i.e., |E| = (|V|2).

8

9.1 Definition• Adjacency list representation

– For each vertex, keep a list of all adjacent vertices.

– For sparse graphs.

– Space requirement is (|E|+|V|).

9

9.1 Definition

1 2 4 3 null

2 4 5 null

3 6 null

4 6 7 3 null

5 4 7 null

6 null

7 6 null

Figure 9.2 An adjacency list representation of a graph

10

9.2 Topological Sort

• An ordering of vertices in a directed acyclic graph, such that if there is a path from vi to vj, then vj appears after vi in the ordering. v1 v2

v3 v4 v5

v6 v7

Figure 9.4 An acyclic graph

11

9.2 Topological Sort

• Topological ordering is not possible if there is a cycle in the graph.

• A simple algorithm– Compute the indegree of all vertices from the

adjacency information of the graph.

– Find any vertex with no incoming edges.

– Print this vertex, and remove it, and its edges.

– Apply this strategy to the rest of the graph.

– Running time is (|V|2).

12

9.2 Topological Sort

/* Figure 9.5 Simple topological sort */

void Topsort (Graph G)

{

int Counter;

Vertex V, W;

for (Counter=0; Counter<NumVertex; Counter++)

{

V = FindNewVertexOfDegreeZero ();

13

9.2 Topological Sort

if (V == NotAVertex)

{ Error ("Graph has a cycle");

break;

}

TopNum [V] = Counter;

for each W adjacent to V

Indegree [W] --;

}

}

14

9.2 Topological Sort

• An improved algorithm– Keep all the unassigned vertices of indegree

0 in a queue.

– While queue not empty• Remove a vertex in the queue.• Decrement the indegree of all adjacent vertices.• If the indegree of an adjacent vertex becomes 0,

enqueue the vertex.

– Running time is (|E|+|V|).

15

9.2 Topological Sort

Indegree Before Dequeue #Vertex 1 2 3 4 5 6 7v1 0 0 0 0 0 0 0v2 1 0 0 0 0 0 0v3 2 1 1 1 0 0 0v4 3 2 1 0 0 0 0v5 1 1 0 0 0 0 0v6 3 3 3 3 2 1 0v7 2 2 2 1 0 0 0

Enqueue v1 v2 v5 v4 v3, v7 v6

Dequeue v1 v2 v5 v4 v3 v7 v6

Result of applying topological sort to the graph of Figure 9.4

16

9.3 Shortest Path Algorithms/* Figure 9.7 Pseudocode for Topological sort */

void Topsort (Graph G)

{ Queue Q;

int Counter = 0;

Vertex V, W;

Q = CreateQueue (NumVertex);

MakeEmpty (Q);

for each vertex V

if (Indegree [V] == 0)

17

9.3 Shortest Path Algorithms Enqueue (V, Q);

while (!IsEmpty (Q))

{

V = Dequeue (Q);

TopNum [V] = ++Counter; /* Next No. */

for each W adjacent to V

if (--Indegree [W] == 0)

Enqueue (W, Q);

}

18

9.3 Shortest Path Algorithms

if (Counter != NumVertex)

Error ("Graph has a cycle");

DisposeQueue (Q); /* Free the memory */

}

19

9.3 Shortest Path Algorithms

Single-Source Shortest Path Problem

• Given an input a weighted graph, G = (V, E), and a distinguished vertex, s, find the shortest weighted path from s to every other vertex in G.

• Assume there is no edge with a negative cost (which could create negative cost cycles).

20

9.3 Shortest Path Algorithms

Unweighted Shortest Paths for Single-Source Graphs

• A simple algorithm1. Mark the starting vertex, s

2. Find all unmarked vertices adjacent to s (vertices adjacent to s)

3. Find all unmarked vertices adjacent to the vertices marked in 2.

4. Repeat Step 3. until no more vertices can be marked.

21

9.3 Shortest Path Algorithms

• The strategy is known as breadth-first search.

• For each vertex, it is required to keep track of – whether the vertex has been marked (known)

– its distance from s (dv)

– previous vertex of the path from s (pv)

• Running time is (|V|2).

22

9.3 Shortest Path Algorithms

v Known dv pvv1 0 0v2 0 0v3 0 0 0v4 0 0v5 0 0v6 0 0v7 0 0

Initial configuration for Fig. 9.4 (Slide 10)

23

9.3 Shortest Path Algorithms

/* Fig. 9.16 Unweighted shortest path algorithm */

void Unweighted (Table T)

/* Assume T is initialized */

{ int CurrDist;

Vertex V, W;

for (CurrDist=0; CurrDist<NumVertex; CurrDist++)

for each vertex V

24

9.3 Shortest Path Algorithms if (!T [V].Known && T [V].Dist

== CurrDist)

{ T [V].Known = True;

for each W adjacent to V

if (T [W].Dist == Infinity)

{ T [W].Dist = CurrDist + 1;

T [W].Path = V;

}

}}