Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph Theory Arnold Mesa. Basic Concepts n 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.

Similar presentations


Presentation on theme: "Graph Theory Arnold Mesa. Basic Concepts n 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."— Presentation transcript:

1 Graph Theory Arnold Mesa

2 Basic Concepts n 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 edges

3 Basic Concepts (cont.) n A vertex (v1) is said to be adjacent to (v2) if and only if they share a common edge (e1). v1v3v2 e1e2

4 Basic Concepts (cont.) n A directed graph or digraph is a graph where direction matters. n Movement in the graph is determined by the direction of the edge. v1 v5 v3 v6 v2 v4

5 Basic Concepts (cont.) n A path in a graph is a sequence of vertices (v1, v2, v3,…,v n ). n The path length is determined by how many edges are on the path. v1 v5 v3 v6 v2 v

6 Adjacency List Representation v1 v5 v3 v6 v2 v4 v1v2 v2v3 v6 v3v4 v6 v4v3v5 v5v6 v6v1 Bi-Directional paths are allowed

7 More terms… weightcost n An edge may have a component known as a weight or cost associated to it. v1v3v2 46

8 Adjacency Matrix Representation v1 v5 v3 v6 v2 v v1v3v4v2 v1 v5v6 v5 v4 v3 v2 v

9 Graph Algorithms n Shortest-Path Algorithms Unweighted Shortest Paths Floyd’s Algorithm Dijkstra’s Algorithm All-Pairs Shortest Path n Network Flow Problems Maximum-Flow Algorithm n Minimum Spanning Tree Prim’s Algorithm Kruskal’s Algorithm

10 Shortest Path Algorithms n Unweighted Shortest Paths ss –Starting with a vertex s, we find the shortest path from s to all the other vertices v1 v7v6 v5 v2 v4 v3 s

11 Shortest Path Algorithms n Floyd’s Algorithm –Suppose we have the following adjaceny matrix v1v3v4v2 v1 v4 v3 v v1 v3 v2 v

12 v1v3v4v2 v1 v4 v3 v 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.)

13 v1v3v4v2 v1 v4 v3 v How about (v2, v4)? (1 + 2) = 3 Since this is less than 7, we replace the 7 with a 3. v1v3v4v2 v1 v4 v3 v Matrix Addition

14 What is the purpose of doing this?

15 v1v3v4v2 v1 v4 v3 v v1 v3 v2 1 7 v4 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.

16 v1v3v4v2 v1 v4 v3 v v1v3v4v2 v1 v4 v3 v v1v3v4v2 v1 v4 v3 v v1v3v4v2 v1 v4 v3 v Now do the same algorithm with row and column v2. After row and column v3. 5

17 v1v3v4v2 v1 v4 v3 v v1v3v4v2 v1 v4 v3 v Finally, after row and column v4. 5 Although we did not see it in this example. It is possible a square gets updated more than once. After Floyd’s Algorithm is complete, we have the shortest path/cost path from any node to any other node in the graph. Time Complexity: O(V 3 )

18 Shortest Path Algorithms n Dijkstra’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 v s Greedy path

19 Dijkstra’s Algorithm s Pick a starting point s and declare it known (True). We sill start with v1. known d v p v known is a true/false value. Whether we have delclared a vertex known (true) or not (false) d v is the cost from the previous vertex to the current vertex p v is the previous vertex in the path to the current vertex V v1 v2 v3 v4 v1 v3 v2 v4 s T 0 0 F 0 0

20 known d v p v V v1 v2 v3 v4 F 0 0 v1 v3 v2v 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 d v and p v We have a choice now. Do we pick v2 or v4 to explore? known d v p v V v1 v2 v3 v4 T 0 0 F 9 v1 F 13 v1 F 2 v1

21 v1 v3 v2v We pick v4. Why? Greedy 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 d v p v V 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

22 v1 v3 v2v 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 d v p v V 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.

23 v1 v3 v2v 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 d v p v V v1 v2 v3 v4 T 1 v2 T 2 v3 T 4 v4 T 2 v1

24 So How Do We Use This? known d v p v V 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 p v column. Work backwards to the vertex you want to end up in. Easy! Lets look at an example: Let’s say we want the shortest path from v1 to v2. We see that v1 goes into v4 at a cost of 2. Now v4 goes into v3 at a cost of 4. Lastly v3 goes into v2 at a cost of = 8

25 v1 v3 v2v If we took a direct path from v1 to v2 it would cost us 9. But we found a way to get from v1 to v2 at a cost of 8. Time complexity of Dijkstra’s Algorithm is O(V 2 ) known d v p v V v1 v2 v3 v4 T 1 v2 T 2 v3 T 4 v4 T 2 v1

26


Download ppt "Graph Theory Arnold Mesa. Basic Concepts n 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."

Similar presentations


Ads by Google