cs 311 graph algorithms. definitions a graph g = (v, e) where v is a set of vertices and e is a set...

27
CS 311 Graph Algorithms

Post on 19-Dec-2015

217 views

Category:

Documents


3 download

TRANSCRIPT

CS 311Graph Algorithms

Definitions

• A Graph G = (V, E) where V is a set of vertices and E is a set of edges,

• An edge is a pair (u,v) where u,v V.

• If the edges are ordered the graph is called directed. Sometimes called a Digraph.

• Vertex v is adjacent to vertex u iff (u,v) E.

• A weighted edge has a cost or weight associated with it.

• A path is a sequence of vertices v1, v2, ... vn such that (vi,vi+1) E for 1 i n

• The length of the path is the number of edges, which is n-1

• Some special cases: path of length 0, and a loop.

• A simple path is one in which each vertex is distinct, except possibly the first and last

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

• A directed acyclic graph (DAG) is a directed graph containing no cycles.

• Connected graph: undirected graph where each node is reachable from every other node.

• Strongly connected: A directed graph which is connected.

• Weakly connected: a directed graph where the underlying undirected graph is connected.

• A graph is completely connected if there is an edge from every vertex to every other vertex.

• Graph S = (V’, E’ ) is a subgraph of graph G = (V, E) iff V’ V and E’ E

• Indegree and Outdegree of a vertex is the number of edges coming in and going out respectively

• A graph is dense if |E| = (|V|2). Otherwise, a graph is sparse.

Representing Graphs

• Pictorially using dots for vertices and lines for edges.

• Adjacency Matrix for dense graphs: use a |V| by |V| matrix A where A [u][w] = 1 if (u,w) E and 0 otherwise. Also nonzero entries can represent weights.

• Adjacency List for sparse graphs.

Graph Traversals

• A tree is a graph (DAG)

• Just as we can traverse a tree in a depth-first or a breadth-first manner, so we can traverse a graph in a breadth-first or depth-first manner.

Depth-First Traversal

Algorithm DFS( Graph G, Vertex V )Visit( V )

Mark( V )

For each vertex W adjacent to V

If W is not marked

DFS( G, W )

Breadth-First Traversal

Algorithm BFT( Graph G, Vertex S )Mark(S); Visit (S); Q.Enqueue(S)

While the Queue is not empty

V = Q.Dequeue()

For each vertex W adjacent to V that is not marked

Mark( W )

Visit( W )

Q.Enqueue( W )

Topological Sort• A topological sort of a directed acyclic

graph is an ordering of the vertices such that vertex w appears after vertex v in the ordering if there is a path from v to w.

• A simple algorithm is to find each vertex of indegree 0, print them, remove the vertices and all their edges from the graph, and repeat the procedure. This algorithm can be implemented by a depth-first traversal.

• A better way is to use a queue.– First put all vertices with indegree 0 in the

queue.– While the queue is not empty, dequeue a node,

print it out, and decrement the indegrees of all its adjacent nodes.

– If the indegree of a vertex becomes 0, put it into the queue.

– If the queue becomes empty before all nodes are printed, the graph contains a cycle.

Single-Source Shortest Paths

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

• In this version, we traverse the graph in a breadth-first manner, and assume initially that each node has distance from s.

• Pseudocode for shortest path (unweighted)

• First enqueue s

• While the queue is not empty– remove a vertex v from the queue– for each vertex w that is adjacent to v

• if w’s distance from s is infinity– set the distance to 1 + v’s distance– enqueue w

Dijkstra’s Algorithm

• This algorithm is for the single-source shortest path problem in a weighted graph having no negative cost edges.

• This is an example of a greedy algorithm.– At each stage, the vertex v having the shortest

distance from s is selected from the unknown vertices. Then, the path from s to v is declared known.

Dijkstra’s Algorithm (Edsger Dijkstra)Input: D[], Graph G, Vertex S

D[S] = 0

while there are unmarked vertices

V=least distance unmarked vertex

if D[V] = STOP: V is not reachable

mark V

for each vertex W adjacent to V

if W is not marked

if D[V] + d(V,W) < D[W]

D[W] = D[V] + d(V,W)

Negative CostsBellman-Ford

• Dijkstra’s algorithm does not work if there are negative cost edges.

• The Bellman-Ford algorithm uses a Queue (Q) and keeps track of the vertices already in queue. Again, the initial node is S.

Q.enqueue( S )

while Q is not empty

V = Q.dequeue()

For each vertex W adjacent to V

if D[V] + d(V,W) < D[W]

D[W] = D[V] + d(V,W)

if W is not in Q,

Q.enqueue(W)

Negative Cost Cycles

• The preceding works if there are negative cost edges, but not if there are negative cost cycles

• For negative cost cycles, we must keep track of how many times a vertex is put into the queue.

• The algorithm terminates if any vertex is dequeued |V| + 1 times

All-Pairs Shortest Path

• Find the shortest distance between all pairs of vertices in the graph. This is the Floyd-Warshall algorithm.

• We may assume negative-cost edges, but not negative-cost cycles.

• This is a dynamic-programming solution that runs in O( |V|3 ) time.

Floyd-Warshall I

Let G( V, E ) be a graph of N nodes

W[N+1][N+1] is the weight matrix, where for two nodes, i and j:

W[i][j] = 0, if i = j

W[i][j] = wi,j if (i,j) is an edge

W[i][j] = if i j and (i,j) is not an edge

Floyd-Warshall II

Let P[N+1][N+1] be a predecessor matrix, where for two nodes, i and j:

P[i][j] = NOT_A_VERTEX, if i = j or if W[i][j] =

P[i][j] = i, if W[i][j] <

Floyd-Warshall IIIfor k = 1 to N

for i = 1 to N

for j = 1 to N

if( W[i][j] > W[i][k] + W[k][j] )

W[i][j] = W[i][k] + W[k][j]

P[i][j] = P[k][j]

Minimum Cost Spanning Tree

• Given a undirected graph G, a MCST is a tree that connects each node of graph with a total path length that is a minimum.

• There are two basic algorithms, both of which are greedy algorithms

• Prim’s Algorithm

• Kruskal’s Algorithm

Prim’s Algorithm

• Grow the tree one vertex at a time.

• At any given time, the graph is divided into two sets of vertices: those in the tree (A) and those not (B).

• At each stage, add the least cost edge that connects a vertex in set A to a vertex in set B. Then add this vertex to set A.

Kruskal’s Algorithm

• Greedy Strategy• Select edges in order of smallest cost,

adding an edge if it does not cause a cycle• Initially, there is a forest of |V| single-node

trees. Each stage adds an edge: merges two trees.

• When there is a single tree, the algorithm finishes.

1 2

3 4 5

6 7

2

4

2

1 3

2

10

6

1

8 45

Directed Example

1 2

3 4 5

6 7

2

4

2

1 3

7

10

6

1

8 45

Undirected Example