graph theory arnold mesa. basic concepts n a graph g = (v,e) is defined by a set of vertices and...

Post on 14-Dec-2015

222 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Graph Theory

Arnold Mesa

Basic Concepts

A graph G = (V,E) is defined by a set of vertices and edges

v3

v1 v2Vertex (v1)

Edge (e1)

A Graph with 3 vertices and 3 A Graph with 3 vertices and 3 edgesedges

Basic Concepts (cont.)

A vertex (v1) is said to be adjacent to (v2) if and only if they share a common edge (e1).

v1 v3v2

e1 e2

Basic Concepts (cont.)

A directed graph or digraph is a graph where direction matters.

Movement in the graph is determined by the direction of the edge.

v1

v5

v3

v6

v2

v4

Basic Concepts (cont.)

A path in a graph is a sequence of vertices (v1, v2, v3,…,vn).

The path length is determined by how many edges are on the path.

v1

v5

v3

v6

v2

v4

1

2

3

Adjacency List Representation

v1

v5

v3

v6

v2

v4

v1 v2

v2 v3 v6

v3 v4 v6

v4 v3 v5

v5 v6

v6 v1

Bi-Directional paths are allowed Bi-Directional paths are allowed

More terms…

An edge may have a component known as a weightweight or costcost associated to it.

v1 v3v2

44 66

Adjacency Matrix Representation

v1

v5

v3

v6

v2

v4

44

11

33

44

66

44

77

66

v1 v3 v4v2

v1

v5 v6

v5

v4

v3

v2

v6

0

0

0

0

0

0

0 0 0

00 00

0 0 3355

0 00 0

00 0 0

0 000

66

66

11 44

77

66

44

66

Graph Algorithms

Shortest-Path AlgorithmsShortest-Path Algorithms• Unweighted Shortest Paths• Floyd’s Algorithm• Dijkstra’s Algorithm• All-Pairs Shortest Path

Network Flow ProblemsNetwork Flow Problems• Maximum-Flow Algorithm

Minimum Spanning TreeMinimum Spanning Tree• Prim’s Algorithm• Kruskal’s Algorithm

Shortest Path Algorithms

Unweighted Shortest PathsUnweighted Shortest Paths– Starting with a vertex ss, we find the shortest path from ss to

all the other vertices

v1

v7v6

v5

v2

v4

v3

ss

11

00

11 22

22

Shortest Path Algorithms

Floyd’s AlgorithmFloyd’s Algorithm– Suppose we have the following adjaceny matrix

v1 v3 v4v2

v1

v4

v3

v2

0

0

0

0

1 7

213

13 2

7 411

9

10

14

v1

v3

v2 v4

9

1

7

13

v1 v3 v4v2

v1

v4

v3

v2

0

0

0

0

1 7

213

13 2

7 411

9

10

14

Matrix Addition

• We first look at row and column v1

• Then we look at each square v’ not in row or column v1 (for example look at (v2,v3) = 10)

• Now look at the corresponding row and column in v1. (1, 13)

• Add the two numbers together. (1+13 = 14)

• If the sum is less than v’, replace v’ with the sum. (Is 14 < 13? No, so don’t replace it.)

v1 v3 v4v2

v1

v4

v3

v2

0

0

0

0

1 7

213

13 2

7 411

9

10

14

• How about (v2, v4)? (1 + 2) = 3

• Since this is less than 7, we replace the 7 with a 3.

v1 v3 v4v2

v1

v4

v3

v2

0

0

0

0

1 3

213

13 2

7 411

9

10

14

Matrix Addition

What is the purpose of doing What is the purpose of doing this?this?

v1 v3 v4v2

v1

v4

v3

v2

0

0

0

0

1 7 3

213

13 2

7 411

9

10

14

v1

v3

v2

1

7v4

2

If we take the path from v2 to v4 it would cost us a total of 7 units

However, if we move from v2 to v1. Then move from v1 to v4. The total cost would be 3 units.

We have essentially found the shortest path at this moment from v2 to v4.

v1 v3 v4v2

v1

v4

v3

v2

0

0

0

0

1 3

213

13 2

7 411

9

10

14

v1 v3 v4v2

v1

v4

v3

v2

0

0

0

0

1 3

213

3 2

7 411

9

10

5

v1 v3 v4v2

v1

v4

v3

v2

0

0

0

0

1 3

213

3 2

7 411

9

10

v1 v3 v4v2

v1

v4

v3

v2

0

0

0

0

1 3

213

3 2

6 47

9

10

5

Now do the same algorithm with row and column v2.Now do the same algorithm with row and column v2.

After row and column v3.After row and column v3.

5

v1 v3 v4v2

v1

v4

v3

v2

0

0

0

0

1 3

213

3 2

6 47

9

10

v1 v3 v4v2

v1

v4

v3

v2

0

0

0

0

1 3

26

3 2

6 47

8

7

5

Finally, after row and column v4.Finally, after row and column v4.

5

Although we did not see it in this example. It is Although we did not see it in this example. It is possible a square gets updated more than once. possible a square gets updated more than once.

After Floyd’s Algorithm is complete, we have the After Floyd’s Algorithm is complete, we have the shortest path/cost path from any node to any other shortest path/cost path from any node to any other node in the graph. node in the graph.

Time Complexity: O(VTime Complexity: O(V33))

Shortest Path Algorithms

Dijkstra’s AlgorithmDijkstra’s Algorithm– Example of a greedy algorithm.– A greedy algorithm is one that takes the shortest path at any

given moment without looking ahead. Many times a greedy algorithm is not the best choice.

v1 v4

v3

v211

22 22

99

ss

Greedy pathGreedy path

Dijkstra’s Algorithm

Pick a starting point ss and declare it known (True). We sill start with v1.

known dv pv

known is a true/false value. Whether we have delclared a vertex known (true) or not (false)

dv is the cost from the previous vertex to the current vertex

pv is the previous vertex in the path to the current vertex

V

v1

v2

v3

v4

v1

v3

v2 v4

ss

T 0 0

F 0 0

F 0 0

F 0 0

known dv pvV

v1

v2

v3

v4

F 0 0

F 0 0

F 0 0

F 0 0

v1

v3

v2 v4

9

13

7

1

10 14

11

2

2 4

v1 goes to v2 and v4. What is the cost of each edge?

v1 to v2 = 9, v1 to v4 = 2, and v1 to v3 = 13

Now we declare 1 as known (True)

and update dv and pv

We have a choice now. We have a choice now.

Do we pick v2 or v4 to explore?Do we pick v2 or v4 to explore?

known dv pvV

v1

v2

v3

v4

T 0 0

F 9 v1

F 13 v1

F 2 v1

v1

v3

v2 v4

9

13

7

1

10 14

11

2

2 4

We pick v4. Why?

Greedy AlgorithmGreedy Algorithm

We want the shortest path.

v4 goes into v3 and v2

v4 to v3 = 4

v4 to v2 = 7

Now v4 is declared known.

We update the table like before.

known dv pvV

v1

v2

v3

v4

T 0 0

F 7 v4

F 4 v4

T 2 v1

Next we explore the next vertex that led from v1. Namely v2

v1

v3

v2 v4

9

13

7

1

10 14

11

2

2 4

v2 goes to v1 and v3

v2 to v1 = 1

v2 to v3 = 10

Now we declare v2 as known and update the table again.

known dv pvV

v1

v2

v3

v4

T 1 v2

T 7 v4

F 4 v4

T 2 v1

•But wait! There is a problem.

•When updating v3 we see that the cost 10 is higher than the cost 4.

•We like the cost 4 so we will keep v3 the same.

v1

v3

v2 v4

9

13

7

1

10 14

11

2

2 4

The last vertex to look at is v3.

v3 goes to v2, v1 and v4.

v3 to v2 = 2

v3 to v1 = 13

v3 to v4 = 14

v3 is now declared known.

Like before we update as before, keeping in mind to keep lower cost paths.

known dv pvV

v1

v2

v3

v4

T 1 v2

T 2 v3

T 4 v4

T 2 v1

So How Do We Use This?known dv pvV

v1

v2

v3

v4

T 1 v2

T 2 v3

T 4 v4

T 2 v1•Take any vertex you want as a starting point.

•Now locate that vertex in the pv column.

•Work backwards to the vertex you want to end up in.

Easy!Easy!

Lets look at an example:Lets look at an example:

Let’s say we want the shortest path from v1 to v2. Let’s say we want the shortest path from v1 to v2.

We see that v1 goes into v4 at a cost of 2.We see that v1 goes into v4 at a cost of 2.

Now v4 goes into v3 at a cost of 4.Now v4 goes into v3 at a cost of 4.

Lastly v3 goes into v2 at a cost of 2.Lastly v3 goes into v2 at a cost of 2.

2 + 4 + 2 = 82 + 4 + 2 = 8

v1

v3

v2 v4

9

13

7

1

10 14

11

2

2 4

If we took a direct path from v1 to If we took a direct path from v1 to v2 it would cost us v2 it would cost us 99..

But we found a way to get from v1 But we found a way to get from v1 to v2 at a cost of to v2 at a cost of 8.8.

Time complexity of Dijkstra’s Time complexity of Dijkstra’s Algorithm is O(VAlgorithm is O(V22))

known dv pvV

v1

v2

v3

v4

T 1 v2

T 2 v3

T 4 v4

T 2 v1

top related