Docsity

Graph Algorithms - Data Structures - Exam Review Notes | CS 310, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
CS 310 Graph Algorithms, Page 1
'
&
$
%
Shortest Path Problem
Task: given a graph G, find the shortest path from a vertex uto a
vertex v.
•If all edges are of the same length, then BFS will do.
•But some times edges are not equal.
☞We associate a non-negative integer, called weight, with
each edge.
☞The weight of a path is the sum of the weights of all edges
in the path.
☞We want to find a minimum-weight path from uto v.
CS 310 Graph Algorithms, Page 2
'
&
$
%
Let G= (V, E ) be a graph where Vcomprises a set of cities
interconnected by a set of roads (edges) in E.
•If the weight of a road (u, v) represents the distance from uto
v, then a minimum weight x-ypath is the shortest way to get
to yfrom x.
•If the weight of a road (u, v) represents the fee for using the
road (u, v), then a minimum weight x-ypath is the cheapest
way to get to yfrom x.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Graph Algorithms - Data Structures - Exam Review Notes | CS 310 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Shortest Path Problem

Task: given a graph G, find the shortest path from a vertex u to a vertex v.

  • If all edges are of the same length, then BFS will do.
  • But some times edges are not equal.
    • We associate a non-negative integer, called weight, with each edge.
    • The weight of a path is the sum of the weights of all edges in the path.
    • We want to find a minimum-weight path from u to v.

CS 310 Graph Algorithms, Page 2 ' $

Let G = (V, E) be a graph where V comprises a set of cities interconnected by a set of roads (edges) in E.

  • If the weight of a road (u, v) represents the distance from u to v, then a minimum weight x-y path is the shortest way to get to y from x.
  • If the weight of a road (u, v) represents the fee for using the road (u, v), then a minimum weight x-y path is the cheapest way to get to y from x.

Dijkstra’s Shortest Path Algorithm

  • Actually, it is a one-to-all shortest paths algorithm — given a starting vertex u, the algorithm finds the shortest paths from u to all the other nodes that are reachable from u.
  • Surprisingly, the one-to-one shortest path problem is no easier than the one-to-all problem, meaning that Dijkstra’s algorithm is probably among the best we could find.
  • Dijkstra’s algorithm is specified by many important networking standards for the computation of paths to deliver computer communication messages.
    • in the Internet, the OSPF protocol.

CS 310 Graph Algorithms, Page 4 ' $

Example

0

1

5

2

3

4

The Predecessors Array

0

1

5

2

3

4

2 9 6

15

8 1 3 7

3

0 1 2 3 4 5 Pred

Whenever dist[v] is set to dist[u] + weight(u, v), P red[v] is set to u.

CS 310 Graph Algorithms, Page 8 ' $

A Second Example

0

(^1 )

3

4 5 6

3

8

5

7

4

8

2

(^0 1 2 3 4 ) Pred

6

0 1 2 3 4 5 6 Dist

Minimum Spanning Tree Problem

Let G = (V, E) be a graph where V is a set of cities and E a set of planned, but not yet constructed, roads to connect cities in V. Let the weight of a road/edge be the cost to construct the road. Problem: using minimum cost, construct a set of roads to connect all the cities. 0

3

8

5

7

4

8

2

(^615)

CS 310 Graph Algorithms, Page 10 ' $

  • For the minimum spanning tree problem, we focus on undirected graphs.
  • An undirected graph with N vertices is a tree if it is connected and contains N − 1 edges.

Example

Pred

0 1 2 3 4 5 6

0 1 2 3 4 5 6 Cost

0

(^1 )

3

4 5 6

3

8

5

7

4

8

2

(^615)

CS 310 Graph Algorithms, Page 14 ' $

Representing Weighted Graphs

  • In the adjacency table representation, edges[u][v] = − 1 indicates no edge from u to v, and edges[u][v] = w ≥ 0 indicates an u to v edge with weight w.
  • In the adjacency list representation, add a weight field to each list node.

0

1

5

2

3

4

2 9 6 15

(^81 ) 7

3

neighbor weight next 1 2 5 9 5 6 3 15 2 8 3 1

3 3 2 7 4 3

0 1 2 3 4 5

CS 310 Graph Algorithms, Page 16 ' $

Directed Acyclic Graph (DAG)

A DAG is a directed graph containing no cycles.

A

D

B

C E

V = {A,B,C,D,E}

E = {(A,C),(A,D),(A,E)

(B,E),(C,B),(C,D),

(E,D)}

Topological Sort

  1. ∀ vertices v, compute predv , the number of tasks that vertex depends on (directly).
  2. Find a vertex w where predw = 0. Output w.
  3. ∀ vertices x where w → x, decrement predx
  4. repeat steps 2 and 3 until no more vertices left in the graph.

CS 310 Graph Algorithms, Page 20 ' $

Example