graph definitions and applications. graphs: very useful abstractions graphs apply to many diverse...
TRANSCRIPT
Graph Definitions and Applications
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.
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
FT
F
Sequence If-then-else While Repeat-until Case
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
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
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?
Interpreters at the Great World Conference
Swedish—(
Booth #1 Booth #2 Booth #3 Booth #4
)—Indonesian
(1)PortugueseIndonesian
(2)Polish
EnglishGerman
(3)Italian
NorwegianEnglish
(4)KoreanTurkish
(5)EnglishPolish
(6)SwedishTurkish
(7)SpanishChinese
Japanese
(8)French
Portuguese
(9)Dutch
German
(10)Swedish
Norwegian
(11)Japanese
IndonesianRussian
(12)DutchFrench
ChineseRussian
Portuguese
(13)PolishItalian
Swedish
(14)GermanChineseKorean
(15)Dutch
SpanishIndonesian
(16)FrenchEnglish
Table of Spoken LanguagesA 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 B
Chinese 1 1 1
Dutch 1 1 1 1
English 1 1 1 1
French 1 1 1
German 1 1 1
Indonesian 1 1 1 1
Italian 1 1
Japanese 1 1
Korean 1 1
Norwegian 1 1
Polish 1 1 1
Portuguese 1 1 1
Russian 1 1
Spanish 1 1
Swedish 1 1 1 1
Turkish 1 1
A 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 B
A 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 1 0 0 1 1 0 0 1 0 1
2 0 0 0 0 0 1 0 0 0 1 0 0 0 1 1 0 0 0
3 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 1 0
4 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0
5 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0
6 1 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0
7 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0
8 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0
9 0 0 1 0 0 0 0 0 0 0 0 0 1 0 1 1 0 0
10 1 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0
11 0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 1
12 0 1 0 0 0 0 0 1 1 1 0 1 0 0 1 1 1 0
13 1 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0
14 0 0 1 0 1 0 0 1 0 1 0 0 1 0 0 0 0 0
15 0 1 0 0 0 0 0 1 0 1 0 1 1 0 0 0 0 1
16 0 0 0 1 0 1 0 0 1 0 0 0 1 0 0 0 0 0
B 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0
“Can Speak to” Matrix
Graph of “Can Speak to” Matrix
10 3Norwegian English
2
Eng
lish
English
13Polish
Swed
ish
6
Swedish
Sw
edis
h
5
Polish
Polis
h
English
Eng
lish
14
Ger
man
9German
Germ
an
4
Turkish
Korean
7
Dutch
Dutch
DutchChinese
French
Chinese Chinese
Span
ish
8
1
11
Port
ugue
se
Portuguese
Portuguese
French
Japa
nese
Indonesian
Indonesian
Rus
sian
Indo
nesi
an
Italian
Swedish
Indonesian15
12
A B
Indonesian
Indo
nesi
an
Swedish
Swedish
16
French
Graph Definition• Hyp: edges either all ordered or all unordered
G = (V, E), whereV is a set of vertices/nodes, andE 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)}
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
A graph is planar if and only if it does not contain either K5 or B3 as a
subgraph.
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 1010v4
1000v3
1100v2
0010v1
v4v3v2v1
A =
Powers of Adjacency Matrices
• Powers of adjacency matrices: A2, A3, …• Can compute powers
– Using ordinary arithmetic:
– Using Boolean arithmetic:
aij = k=1
n
aikakj
aij = k=1
n
aikakj
1 2
3
4
Powers Using Ordinary Arithmetic
10104
10003
11002
00101
4321
A =
21104
10103
20102
11001
4321
A2 =
41204
21103
31202
20101
4321
A3 =
<2,3,4> <2,4,4>
<4,2,4> <4,4,4>
<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>
<1,2,4>
<3,4,4>
Powers Using Ordinary Arithmetic (continued…)
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.
72404
41203
62302
31201
4321
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>
1 2
3
4
Powers Using Ordinary Arithmetic (continued…)
72404
41203
62302
31201
4321
10104
10003
11002
00101
4321
A =
21104
10103
20102
11001
4321
A2 =
41204
21103
31202
20101
4321
A3 =
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
0 + 1 + 2 =
1 + 2 + 4 =
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:
R = A;for (i = 2 to n){ compute Ai; R = R Ai;}O(n4)
(n-1)nnn
for (j = 1 to n){ for (k = 1 to n) { ai
jk = 0;
for (m = 1 to n) { ai
jk = aijk (ai-1
jm a1mk);
} }}
1 2
3
4
Reachability
10104
10003
11002
00101
4321
A =
11104
10103
11102
11101
4321
A A2 =
11104
11103
11102
11101
4321
A A2 A3 =
11104
11103
11102
11101
4321
A A2 A3 A4 =
1st iteration 2nd iteration 3rd iteration
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.
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].
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
Proof of Correctness for Reachability
• Loop invariant: After z outer loops, axy = 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 n1 outer loops since i runs from 2 to n. Thus, when the algorithm terminates, if the loop invariant holds, axy = 1 iff y is reachable from x along a path of length (n1)+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 axy is 1 iff there is a path from x to y;
thus axy = 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, axy = 1 iff y is
reachable from x along a path of length z+1 or less from x to y. In the z+1st iteration we compute A(z+2), in which axy = 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, axy = 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 Ai; R = R Ai;}
for (j = 1 to n){ for (k = 1 to n) { ai
jk = 0;
for (m = 1 to n) { ai
jk = aijk (ai-1
jm a1mk);
} }}