Download presentation
Presentation is loading. Please wait.
Published byBasil Barrett Modified over 9 years ago
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 SequenceIf-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 –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 #1Booth #2Booth #3Booth #4 )—Indonesian (1) Portuguese Indonesian (2) Polish English German (3) Italian Norwegian English (4) Korean Turkish (5) English Polish (6) Swedish Turkish (7) Spanish Chinese Japanese (8) French Portuguese (9) Dutch German (10) Swedish Norwegian (11) Japanese Indonesian Russian (12) Dutch French Chinese Russian Portuguese (13) Polish Italian Swedish (14) German Chinese Korean (15) Dutch Spanish Indonesian (16) French English
8
Table of Spoken Languages A12345678910111213141516B Chinese 111 Dutch 1111 English 1111 French 111 German 111 Indonesian 1111 Italian 11 Japanese 11 Korean 11 Norwegian 11 Polish 111 Portuguese 111 Russian 11 Spanish 11 Swedish 1111 Turkish 11
9
A12345678910111213141516B A 000000100010010000 1 000000001001100101 2 000001000100011000 3 000001000010010010 4 000000100000001000 5 001100000000010010 6 100010000010010000 7 000000000001101100 8 010000000000100010 9 001000000000101100 10 100100100000000000 11 010000010000100101 12 010000011101001110 13 101101100000000000 14 001010010100100000 15 010000010101100001 16 000101001000100000 B 010000000001000100 “Can Speak to” Matrix
10
Graph of “Can Speak to” Matrix 103 Norwegian English 2 13 Polish Swedish 6 5 Polish English 14 German 9 4 Turkish Korean 7 Dutch Chinese French Chinese Spanish 8 1 11 Portuguese French Japanese Indonesian Russian Indonesian Italian Swedish Indonesian 15 12 A B Indonesian Swedish 16 French
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 V H is a subset of V G and E H is a subset of E G A graph G is complete if all nodes in G are connected to all other nodes in G (often denoted K 1, …, K n ) A graph G is planar if it can be drawn (in 2D) with no crossing lines K5K5 B3B3 A graph is planar if and only if it does not contain either K 5 or B 3 as a subgraph.
13
Adjacency Matrix Definition: Let G = (V, E) be a simple digraph. Let V = {v 1, v 2,…v n } be the vertices (nodes). Order the vertices from v 1 to v n. The n n matrix A whose elements are given by a ij = 1, if (v i, v j ) E 0, otherwise is the adjacency matrix of the graph G. Example: { 12 3 4 1010 v4v4 1000 v3v3 1100 v2v2 0010 v1v1 v4v4 v3v3 v2v2 v1v1 A =
14
Powers of Adjacency Matrices Powers of adjacency matrices: A 2, A 3, … Can compute powers –Using ordinary arithmetic: –Using Boolean arithmetic: a ij = k=1 n a ik a kj a ij = k=1 n a ik a kj
15
12 3 4 Powers Using Ordinary Arithmetic 1010 4 1000 3 1100 2 0010 1 4321 A = 2110 4 1010 3 2010 2 1100 1 4321 A 2 = 4120 4 2110 3 3120 2 2010 1 4321 A 3 =
16
Powers Using Ordinary Arithmetic (continued…) The element in the ith row and the jth column of A n is equal to the number of paths of length n from the ith node to the jth node. 7240 4 4120 3 6230 2 3120 1 4321 A 4 = 12 3 4
17
Powers Using Ordinary Arithmetic (continued…) 7240 4 4120 3 6230 2 3120 1 4321 1010 4 1000 3 1100 2 0010 1 4321 A = 2110 4 1010 3 2010 2 1100 1 4321 A 2 = 4120 4 2110 3 3120 2 2010 1 4321 A 3 = A 1 + A 2 = # of ways in 2 or fewer A 1 + A 2 + A 3 = # of ways in 3 or fewer A 1 + A 2 + A 3 + A 4 = # of ways in 4 or fewer 0 + 1 + 2 = 1 + 2 + 4 =
18
Powers Using Boolean Arithmetic Using Boolean arithmetic, we compute reachability. We can compute the paths of length 2 by A 2, 3 by A 3 … and of length n by A n. Since the longest possible path we care about for reachability is n, we obtain all paths by A A 2 A 3 … A n. The reachability matrix is the transitive closure of A. Algorithm: R = A; for (i = 2 to n) { compute A i ; R = R A i ; } O(n 4 ) (n-1) n n n for (j = 1 to n) { for (k = 1 to n) { a i jk = 0; for (m = 1 to n) { a i jk = a i jk (a i-1 jm a 1 mk ); }
19
12 3 4 Reachability 1010 4 1000 3 1100 2 0010 1 4321 A = 1110 4 1010 3 1110 2 1110 1 4321 A A 2 = 1110 4 1110 3 1110 2 1110 1 4321 A A 2 A 3 = 1110 4 1110 3 1110 2 1110 1 4321 A A 2 A 3 A 4 = 1 st iteration 2 nd iteration 3 rd iteration
20
Algorithms: Proof of Correctness The algorithm terminates –No loops: obvious –Loops: descending sequence on well-founded poset (intuitively, fixed start and end place) The algorithm produces the correct result –No loops: Reason that steps lead to the desired conclusion –Loops: Find an appropriate loop invariant a T/F condition that stays the same as the loop executes, is based on the number of iterations, and leads to the conclusion we need. Prove by induction that the loop invariant holds as the loop executes.
21
Loop Invariant Example: A[i] i=1 n sum = 0 for i = 1 to n sum = sum + A[i] Loop invariant: After k iterations, sum = A[1] + A[2] + … + A[k]. Induction proof: Basis: k = 0 (initialization, before the loop begins), sum = 0, which is the sum after 0 iterations. (Or, we could start with k=1: After 1 iteration, sum = A[1].) Induction: By the induction hypothesis, after k iterations, sum = A[1] + A[2] + … + A[k]. In the next iteration we add A[k+1]. Thus, after k+1 iterations, sum = A[1] + A[2] + … + A[k+1].
22
Loop Invariant Example: Selection Sort -- A is an array of n distinct integers for i = 1 to n find location j of the smallest integer in A[i] … A[n] swap A[i] and A[j] Loop invariant: After k iterations, A[1], …, A[k] are sorted A[k] < all of A[k+1], …, A[n]. Induction proof: Basis: k = 0 (initialization, before the loop begins), A is an array of n distinct integers which may or may not be sorted. (Or, we could start with k=1: After 1 iteration A[1] is sorted A[1] < all of A[1+1], …, A[n].) Induction: By the induction hypothesis, after k iterations, A[1], …, A[k] are sorted A[k] < all of A[k+1], …, A[n]. Then since A[k] < all of A[k+1], …, A[n], and we find the smallest integer in A[k+1], …, A[n] to swap with A[k+1], A[1], …, A[k+1] are sorted A[k+1] < all of A[(k+1)+1], …, A[n]. 1 2 3 9 6 5 8
23
Proof of Correctness for Reachability Loop invariant: After z outer loops, a xy = 1 iff y is reachable from x along a path of length z+1 or less from x to y. Note: the loop terminates after n 1 outer loops since i runs from 2 to n. Thus, when the algorithm terminates, if the loop invariant holds, a xy = 1 iff y is reachable from x along a path of length (n 1)+1 = n or less from x to y, and thus is reachable since n is the longest a path can be (that starts at a node N, visits all other nodes once, and comes back to N). Inductive proof –Basis: z=0 (i.e., before entering the outer loop), R = A and a xy is 1 iff there is a path from x to y; thus a xy = 1 iff y is reachable from x along a path of length 0+1 = 1 or less from x to y. –Induction: By the induction hypothesis we have that after z outer loops, a xy = 1 iff y is reachable from x along a path of length z+1 or less from x to y. In the z+1 st iteration we compute A (z+2), in which a xy = 1 iff there is a path of length z+1 from x to some node q and an edge from q to y, i.e. a path of length z+2 from x to y, and we add these reachable possibilities for paths of length z+2 to the result R. Hence, after z+1 outer loops, a xy = 1 iff y is reachable from x along a path of length (z+1)+1 = z+2 or less from x to y. R = A; for (i = 2 to n) { compute A i ; R = R A i ; } for (j = 1 to n) { for (k = 1 to n) { a i jk = 0; for (m = 1 to n) { a i jk = a i jk (a i-1 jm a 1 mk ); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.