Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graph Definitions and Applications

Similar presentations


Presentation on theme: "Graph Definitions and Applications"— Presentation transcript:

1 Graph Definitions and Applications

2 Graphs: Very Useful Abstractions
Graphs apply to many diverse areas: social sciences, linguistics, physical sciences, communication engineering, and many more. Why? Because they are a useful abstraction. Abstractions capture the essence of a real-world problem. Abstractions discard irrelevant details and let us focus on the essence of the problem to be solved. Abstractions lend themselves to precise (mathematical) specification. Mathematically defined abstractions lead to correct computer implementations.

3 Graphs in Programming Testing: paths in programs: Call graphs in programs Software products consist of modules that invoke each other Deadlock in operating systems T F T F T F Sequence If-then-else While Repeat-until Case

4 Graphs in Transportation & Product Delivery
Airline connections: getting from here to there Highways with mileage between cities Traveling salesman problem: visit all cities with minimal distance Traffic networks studied by transportation and city planners Pipelines for delivering water or gas

5 Graphs in Networks Social networks Computer networks
Friend of a friend Co-collaborators Computer networks Connect all, minimal number of connections (Minimal) spanning trees, LANs, WANs Scheduling networks Workflow from source to sink Critical path, slack time Earliest starting time, latest completion time

6 Problems that Don’t Initially Look Like Graph Problems
Have you ever had to work your way through a maze of interpreters at an international conference? Swedish and Indonesian delegates wish to talk to each other, but among the sixteen interpreters, not one speaks both Swedish and Indonesian. A way to solve this problem is to form a chain of interpreters. There are, however, only four booths; interpreters must be in booths, and each booth can contain only one interpreter. Problem: Does a solution exist? If so, which four interpreters will you place in the booths?

7 Interpreters at the Great World Conference
Swedish—( Booth #1 Booth #2 Booth #3 Booth #4 )—Indonesian (1) Portuguese Indonesian (2) Polish English German (3) Italian Norwegian (4) Korean Turkish (5) (6) Swedish (7) Spanish Chinese Japanese (8) French (9) Dutch (10) (11) Russian (12) (13) (14) (15) (16)

8 Table of Spoken Languages
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 B Chinese Dutch English French German Indonesian Italian Japanese Korean Norwegian Polish Portuguese Russian Spanish Swedish Turkish

9 “Can Speak to” Matrix A 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 B

10 Graph of “Can Speak to” Matrix
5 Polish Polish 11 English Polish English 13 2 Indonesian Swedish German Swedish English English Indonesian Indonesian 16 Swedish Italian Russian Japanese German English Norwegian French Indonesian A 10 3 15 B Swedish Dutch German Dutch 14 9 Indonesian Indonesian French Swedish Swedish Dutch Chinese 1 Portuguese 6 12 Spanish Korean Portuguese Turkish Chinese French Chinese Portuguese 4 8 7

11 Graph Definition Hyp: edges either all ordered or all unordered
G = (V, E), where V is a set of vertices/nodes, and E is a relation on VxV (E is a set of all-ordered or all-unordered pairs) Example 1 2 3 V = {1, 2, 3} E = {(1, 2), (2, 3), (3, 3)}

12 A Few Special Graphs H is a subgraph of G if VH is a subset of VG and EH is a subset of EG A graph G is complete if all nodes in G are connected to all other nodes in G (often denoted K1, …, Kn) A graph G is planar if it can be drawn (in 2D) with no crossing lines K5 B3 d = directed u = undirected A graph is planar if and only if it does not contain either K5 or B3 as a subgraph.

13 Adjacency Matrix Definition: Let G = (V, E) be a simple digraph. Let V = {v1, v2,…vn} be the vertices (nodes). Order the vertices from v1 to vn. The n  n matrix A whose elements are given by aij = 1, if (vi, vj)  E 0, otherwise is the adjacency matrix of the graph G. Example: { 1 2 3 4 1 v4 v3 v2 v1 A =

14 Adjacency Lists For directed graphs: b c a a 2 3 b c Simple Notation b
1 a 2 3 b c b c a Simple Notation b a c a b c a 1 1 1 { (a,a), (a,b), (a,c), (b,c), (c,b) } b 1 c 1

15 Adjacency Lists for Undirected and Weighted Graphs
Undirected Graphs: b a c 5 3 7 1 a b c Make each edge (except loops) go both ways. Weighted Graphs: a (a,1) (b,5) (c,3) b (a,5) (c,7) c (a,3) (b,7) add additional field to node node-weight pairs

16 Space: Adjacency Lists vs. Matrices
Space (n vertices and m edges) matrix: n2 + n(vertex-name size) = matrix size + header size matrix can be bits, but bits are not directly addressable list: n(header-node size) + m(list-node size) Sparse: few edges  0 in the extreme case Matrix  fixed size: so no size benefit List  variable size: as little as n(vertex-node size) Dense: many edges  n2 in the extreme case Matrix  fixed size: so no size loss List  variable size: as much as n(header-node size) + n2(list-node size)

17 Operations: Adjacency Lists vs. Matrices
Operations depend on sparse/dense and what’s being done. Examples (n nodes and m edges) Is there an arc from x to y? Matrix: O(1)  check value at (x, y) List: O(n)  index to x, traverse list to y or end Get successor nodes of a node. Matrix: O(n)  scan a row List: O(n)  traverse a linked list Get predecessor nodes of a node. Matrix: O(n)  scan a column List: O(n+m)  traverse all linked lists, which could be as bad as O(n+n2) = O(n2).

18 Powers of Adjacency Matrices
Powers of adjacency matrices: A2, A3, … Can compute powers Using ordinary arithmetic: Using Boolean arithmetic: aij = k=1 n aikakj aij = k=1 n aikakj

19 Powers Using Ordinary Arithmetic (I)
1 2 3 4 1 2 3 4 1 1 A = 2 1 1 3 1 4 1 1 2 1 4 3 A2 = <1,2,4> <2,3,4> <2,4,4> <3,4,4> <4,2,4> <4,4,4> 4 1 2 3 A3 = <1,2,3,4> <1,2,4,4> <2,4,2,4> <2,4,4,4> <2,3,4,4> <3,4,2,4> <3,4,4,4> <4,2,3,4> <4,2,4,4> <4,4,2,4> <4,4,4,4>

20 Powers Using Ordinary Arithmetic (II)
1 2 3 4 7 2 4 1 3 6 A4 = <1,2,3,4,4> <1,2,4,2,4> <1,2,4,4,4> <2,4,2,4,4> <2,4,4,4,4> <2,3,4,2,4> <2,3,4,4,4> <2,4,2,3,4> <2,4,4,2,4> <3,4,4,4,4> <3,4,2,4,4> <3,4,4,2,4> <3,4,2,3,4> <4,4,4,4,4> <4,4,4,2,4> <4,4,2,4,4> <4,2,4,4,4> <4,2,4,2,4> <4,2,3,4,4> <4,4,2,3,4> The element in the ith row and the jth column of An is equal to the number of paths of length n from the ith node to the jth node.

21 Powers Using Ordinary Arithmetic (III)
1 4 3 2 A = A1 + A2 = # of ways in 2 or fewer A1 + A2 + A3 = # of ways in 3 or fewer A1 + A2 + A3 + A4 = # of ways in 4 or fewer 2 1 4 3 A2 = 7 2 4 1 3 6 = WHY????? 4 1 2 3 A3 = =

22 Powers Using Boolean Arithmetic
Using Boolean arithmetic, we compute reachability. We can compute the paths of length 2 by A2, 3 by A3… and of length n by An. Since the longest possible path we care about for reachability is n, we obtain all paths by A  A2  A3  … An. The reachability matrix is the transitive closure of A. Algorithm: for (j = 1 to n) { for (k = 1 to n) { aijk = 0; for (m = 1 to n) { aijk = aijk  (ai-1jm  a1mk); } R = A; for (i = 2 to n) { compute Ai; R = R  Ai; } O(n4) (n-1)nnn

23 Clearly more efficient than our straightforward reachability algorithm
Warshall’s Algorithm A more efficient way to compute reachability. Algorithm: 1. Initialize adjacency matrix A: (For an edge from i to j, aij is set to 1; otherwise, 0.) 2. for (k=1 to n) for (i=1 to n) for (j=1 to n) if (aij = 0) aij  aik  akj Clearly more efficient than our straightforward reachability algorithm O(n3) < O(n4); but does it work?

24 Intuitive Idea We’ll see that after k outer loops, aij = 1 iff there is a path from i to j directly or through {1, 2, …, k}. 3 4 1 5 6 2 for (k=1 to n) for (i=1 to n) for (j=1 to n) if (aij = 0) aij  aik  akj k is called a pivot k=1: a41  a13 adds (4,3) k=2: a32  a26 adds (3,6), a62  a26 adds (6,6) 1 2 3 4 5 6 1 1 1 k=3: a13  a32 adds (1,2), a43  a32 adds (4,2), a43  a36 adds (4,6), a13  a36 adds (1,6) 1 1 1 1 1 1 1 1 k=4 and k=5 add nothing (either no in or no out) k=6: adds (2,5), (3,5), (1,5), (4,5), (2,2) 1

25 Floyd’s Algorithms

26 Shortest Distance in a Weighted Graph
5 2 3 4 1 6 8 Minimum Dist[1,4] = 6 Minimum Dist[3,2] =

27 Variation on Warshall’s Algorithm: Pivot on node k
Floyd’s Algorithm Variation on Warshall’s Algorithm: Pivot on node k Initialize for k=1 to n for i=1 to n for j=1 to n if Dist[i,j] > Dist[i,k] + Dist[k,j] then Dist[i,j]  Dist[i,k] + Dist[k,j] 5 2 3 4 1 6 8 O(n3) 1 2 3 4 5 8 6 Point out that initial matrix is the edge weights or infinity. Initial Dist[i,j] = The initial matrix has the edge weights or infinity

28 Floyd’s Algorithm (continued…)
Initialize for k=1 to n for i=1 to n for j=1 to n if Dist[i,j] > Dist[i,k] + Dist[k,j] then Dist[i,j]  Dist[i,k] + Dist[k,j] 5 2 3 4 1 6 8 Pivot k: k=1: nothing 1 2 3 4 5 8 6 1 2 3 4 5 8 7 6 k=2: 13:3, 15:7 k=1 k=2

29 Floyd’s Algorithm (continued…)
Initialize for k=1 to n for i=1 to n for j=1 to n if Dist[i,j] > Dist[i,k] + Dist[k,j] then Dist[i,j]  Dist[i,k] + Dist[k,j] 5 2 3 4 1 6 8 Pivot k: k=1: nothing 1 2 3 4 5 8 7 6 1 2 3 4 5 6 k=2: 13:3, 15:7 k=3: 14:13+34:6 15:4 24:5 25:3 45:5 44:0 (not changed) k=2 k=3

30 Floyd’s Algorithm (continued…)
Initialize for k=1 to n for i=1 to n for j=1 to n if Dist[i,j] > Dist[i,k] + Dist[k,j] then Dist[i,j]  Dist[i,k] + Dist[k,j] 5 2 3 4 1 6 8 Pivot k: k=1: nothing 1 2 3 4 5 6 1 2 3 4 5 6 k=2: 13:3, 15:7 k=3: 14:13+34:6 15:4 24:5 25:3 45:5 44:0 (not changed) k=4: no changes k=5: nothing k=3 k=4

31 Notes on Warshall’s and Floyd’s Algorithms
Warshall’s and Floyd’s algorithms do not produce paths. Can keep track of paths Update as the algorithms execute Warshall’s and Floyd’s algorithms only work for directed graphs. For undirected graphs, replace each edge with two directed edges Then, run algorithms as given

32 Dijkstra’s Algorithm

33 Dijkstra’s Algorithm Find the shortest weighted path between two given nodes (Easier to find the minimum from one to all) Algorithm b c a e d 5 2 1 10 Initialize D (distance) table Until all nodes are settled, 2a. find smallest distance & settle node 2b. adjust distances in D for unsettled nodes

34 Trace of Dijkstra’s Algorithm
b c a e d 5 2 1 10 1. Initialize D (start=a) 2. Until all nodes are settled: -find smallest distance & settle node -adjust distances in D for unsettled nodes e d c b a settled distance iteration a 1 2 5 1 a,d 2 5 adjust w/d 2 a,d,c 2 5 adjust w/c 4 a,d,c,e 2 4 3 adjust w/e Distances from a if two the same, choose the first one All circled nodes are shortest distances from node a to node x Settled nodes are the shortest distances Can I derive a shortest path? a,d,c,e,b 4 all nodes settled Circled numbers: shortest path lengths from a.

35 Trace with Different Weights
b c a e d 6 2 4 1 10 1. Initialize D (start=a) 2. Until all nodes are settled: -find smallest distance & settle node -adjust distances in D for unsettled nodes e d c b a settled distance iteration a 1 4 6 1 a,d 4 6 adjust w/d 2 a,d,e 2 4 6 adjust w/e 3 a,d,e,c 3 6 adjust w/c Distances from a if two the same, choose the first one All circled nodes are shortest distances from node a to node x Settled nodes are the shortest distances Can I derive a shortest path? 5 a,d,e,c,b 5 4 all nodes settled Circled numbers: shortest path lengths from a.

36 Practice Exercise

37 Big-Oh for Dijkstra’s Algorithm
Adjacency List: b c a e d 5 2 1 10 D(x) a (b,5) (c,2) (d,1) 5 b (a,5) (c,2) 2 c (a,2) (b,2) (d,10) (e,1) 1 d (a,1) (c,10) (e,1) e (d,1) (c,1) Unroll the loop: n + k1 + n + k2 + … + n + k(n-1) = (n1)n = O(n2) = 2(m  # of edges in the initialization) = O(m) 1. Initialize D (start=a) 2. Until all nodes are settled: 2a. find smallest distance & settle node. 2b. adjust distances in D for unsettled nodes. O(n)  visit at most all edges for a node  can’t be more than n O(n)  each node needs to be settled 2a. O(n)  look through D(x) list to find smallest 2b. O(k)  for chosen node, go through list of length k { O(n2) dominates Over all iterations, the k’s add up to 2(m  # of edges in the initialization).

38 Dijkstra’s Algorithm vs. Floyd’s
Dijkstra’s algorithm is O(mlogn) = O(n2logn) in the worst case Floyd’s algorithm is O(n3), but computes all shortest paths Dijkstra’s algorithm can compute all shortest paths by starting it n times, once for each node. Thus, to compute all paths, Dijkstra’s algorithm is O(nmlogn) = O(n3logn) in the worst case Which is better depends on the application Dijkstra’s algorithm is better if only a path from a single node to another or to all others is needed For all paths, Dijkstra’s algorithm is better for sparse graphs, and Floyd’s algorithm is better for dense graphs


Download ppt "Graph Definitions and Applications"

Similar presentations


Ads by Google