0% found this document useful (0 votes)
15 views

Lecture 1

Uploaded by

Melvin Zhang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lecture 1

Uploaded by

Melvin Zhang
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

CMPT405/705 Foundations Qianping Gu

Foundations
• Design and Analysis of Algorithms, an Example (Ch 1).

• Basics of Algorithm Analysis, Computational Models, Computational


Tractability, Asymptotic Order of Growth (Ch 2).

• Graphs, Undirected graphs, Directed graphs, Formulate Problems by Graphs


(Ch 3).

The lecture notes/slides are adapted from those associated with the text book by J. Kleinberg and E.

Tardos.

1
CMPT405/705 Foundations Qianping Gu

Design and Analysis of Algorithms


• Algorithms are a foundation of computer science

• Design and analysis of algorithms


– Identify a problem

– Formulating the problem with certain mathematical precision

– Design an algorithm to solve the problem

– Analyze the algorithm


Prove the correctness of algorithm
Analyze the efficiency of algorithm

2
CMPT405/705 Foundations Qianping Gu

Identify a problem, an example

Stable Matching Problem

• A group of employers are hiring; a group of students are applying to employers


for jobs.

• Each employer has a preference order on the applicants; each student has a
preference order on employers.

• Based on preferences, employers give offers to some applicants; applicants


choose one offer to accept.

• Design a scheme to assign each applicant to an employer so that employer and


applicant pairs are stable.

3
CMPT405/705 Foundations Qianping Gu

• Unstable employer-applicant pairs


– Applicant A had accepted an offer from employer E1 ,
later employer E2 gave A an offer,
A prefers E2 to E1 and retracts the acceptance from E1 .
– Employer E had given an offer to applicant A1 ,
later E received an application from applicant A2 ,
E thinks A2 is better than A1 and retracts the offer to A1 .

• Stable employer-applicant assignment, there is no unstable employer-applicant


pair

4
CMPT405/705 Foundations Qianping Gu

• Stable employer-applicant assignment


Given the preferences of employers and applicants, assign applicants to
employers so that for each employer E and each applicant A who is not
assigned to E , one of the following holds:

1. E prefers every applicant it made an offer to A; or


2. A prefers her current employer to E .

• Example, two employers E1 , E2 and two applicants A1 , A2


E1 prefers A1 to A2 and E2 prefers A1 to A2
A1 prefers E1 to E2 and A2 prefers E2 to E1
{(E1 , A1 ), (E2 , A2 )} is a stable assignment, Conditions 2 is satisfied
{(E1 , A2 ), (E2 , A1 )} is a not stable assignment, neither of Conditions 1&2 is
satisified, (E1 , A2 ) does not satisfy Condition 1 nor 2, (E2 , A1 ) does not
satisfy Condition 2

5
CMPT405/705 Foundations Qianping Gu

• More stable assignment examples


two employers E1 , E2 and two applicants A1 , A2

E1 prefers A1 to A2 and E2 prefers A2 to A1


A1 prefers E2 to E1 and A2 prefers E1 to E2
{(E1 , A1 ), (E2 , A2 )} is a stable assignment, as Condition 1 is satisfied
{(E1 , A2 ), (E2 , A1 )} is a stable assignment, as Condition 2 is satisfied

6
CMPT405/705 Foundations Qianping Gu

Formulate the problem

• Simplify the problem


Each student applies to every employer, each employer accepts one applicant.

• Notation for employer and applicant pairs


Let M= {m1 , .., mn }, W = {w1 , .., wn } and
M × W = {(m, w)|m ∈ M, w ∈ W }.
S ⊆ M × W is a matching if each element of M appears in at most one pair of
S and each element of W appears in at most one pair of S .
S is a perfect matching if each element of M appears in exactly one pair of S
and each element of W appears in exactly one pair of S .

Example: M = {m, m′ }, W = {w, w′ }, S1 = {(m, w)} is a matching,


S2 = {(m, w), (m, w′ )} is not a matching as m appears in more than 1 pair in S2 ,
S3 = {(m, w), (m′ , w′ )} is a perfect matching.

7
CMPT405/705 Foundations Qianping Gu

• Preferences and ranks


Each m ∈ M has a distinct rank fm (w) on every w ∈ W
each w ∈ W has a distinct rank gw (m) on every m ∈ M
Example: M = {m, m′ } and W = {w, w′ }
– fm (w) = 2 and fm (w′ ) = 1, m prefers w to w′
– fm′ (w) = 2 and fm′ (w′ ) = 1, m′ prefers w to w′
– gw (m) = 2 and gw (m′ ) = 1, w prefers m to m′
– gw′ (m) = 1 and gw′ (m′ ) = 2, w′ prefers m′ to m
– In general, bijection fm : W → {1, 2, .., n}, bijection gw : M → {1, 2, .., n}.

If m prefers w to w then fm (w) > fm (w′ ).

If w prefers m to m then gw (m) > gw (m′ ).

8
CMPT405/705 Foundations Qianping Gu

• Instability
Given a matching S ⊆ M × W , (m, w) ∈ (M × W ) \ S is unstable w.r.t.
(with respect to) S if

1. (m, w′ ) ∈ S , w does not appear in S and fm (w) > fm (w′ ) or


2. (m′ , w) ∈ S , m does not appear in S and gw (m) > gw (m′ ) or
3. (m, w′ ) ∈ S, (m′ , w) ∈ S , fm (w) > fm (w′ ) and gw (m) > gw (m′ ).
• Stable matching
A matching S is stable if

1. S is a perfect matching; and


2. there is no instability w.r.t. S:

for every (m, w ), (m′ , w) in S , fm (w′ ) > fm (w) or gw (m′ ) > gw (m).
• Stable employ-applicant assignment: find a stable matching
• Questions
Does there exist a stable matching for every set of preference lists?
Given a set of preference lists, if a stable matching can be computed efficiently?

9
CMPT405/705 Foundations Qianping Gu

Stable matching examples

2 2 fm (w) g w (m) 2
m w m 1 w
1 1 2
1
2 1
1 2
m’ w’ m’ w’
1 2 2 1

2 2 2 2 2 1 m 2 1 w
m w m w m w
1 1 1 2 1 2
1 1
2 1 2 1 1 2 1 2
m’ w’ m’ w’ m’ w’ m’ w’
1 2 1 2 2 1 2 1
Stable matching Stable matching
Stable matching Unstable matching
S={(m,w),(m’,w’)} S’={(m,w’),(m’,w)}
S={(m,w),(m’,w’)} S={(m,w’),(m’,w)}
fm (w)>f m (w’), gw (m)>g w (m’)

10
CMPT405/705 Foundations Qianping Gu

Design algorithm

G-S Algorithm [Gale and Shapley 1962] for Stable Matching

1. Initially, every element is unpaired.

2. For every unpaired m, let Wm = {w|w ∈ W and m has not tried to pair} and
let w ∈ Wm with the largest fm (w);

If w is unpaired then put (m, w) into a pair;



Otherwise (there is a pair (m , w)),
if gw (m) > gw (m′ ) then remove pair (m′ , w) and put (m, w) a pair,
otherwise mark w as that m has tried to pair.

3. Finally, algorithm stops when every one is paired.

11
CMPT405/705 Foundations Qianping Gu

:= ∅; Wm = W for every m ∈ M ; /* every one is unpaired */


Initially, S
while ∃ unpaired m ∈ M do
Choose an unpaired m; Let w ∈ Wm with the largest fm (w);
if w is unpaired then
S := S ∪ {(m, w)}
else

Let (m , w) be the pair in S ;

if gw (m) > gw (m ) then

remove (m , w) from S ; S := S ∪ {(m, w)}
else
Remove w from Wm ;
end if
end if
end while
Return S ;

12
CMPT405/705 Foundations Qianping Gu

Example, M = {m, m′ } and W = {w, w′ },


fm (w) = 2 and fm (w′ ) = 1, fm′ (w) = 2 and fm′ (w′ ) = 1,
gw (m) = 2 and gw (m′ ) = 1, gw′ (m) = 1 and gw′ (m′ ) = 2
Initially, S = ∅, Wm = {w, w′ }, Wm′ = {w, w′ }
Choose m′ , w has the largest fm′ (w) = 2 in Wm′ , w is unpaired, S = {(m′ , w)}
Choose m, w has the largest fm (w) = 2 in Wm , w is paired with m′ ,
gw (m) = 2 > gw (m′ ) = 1, S = (S \ {(m′ , w)}) ∪ {(m, w)} = {(m, w)}
Choose m′ , w has the largest fm′ (w) = 2 in Wm′ , w is paired with m,
gw (m′ ) = 1 6> gw (m) = 2, Wm′ = {w, w′ } \ {w} = {w′ }
Choose m′ , m′ has the largest fm′ (w ′ ) = 1 in Wm′ , w′ is unpaired,
S = S ∪ {(m′ , w′ )} = {(m, w), (m′ , w′ )}
2 2 2 2 2 2 2 2
m w m w m w m w
1 1 1 1 1 1 1 1

2 1 2 1 2 1 2 1
m’ w’ m’ w’ m’ w’ m’ w’
1 2 1 2 1 2 1 2
fm (w) g (m) Choose (m’,w), S={m’,w} Choose (m,w), w is paired Choose (m’,w’),
w
but w prefers m, S={m,w} S={(m,w),(m’,w’)}
Wm ={w,w’} Wm’ ={w,w’}
Choose (m’,w), w is paired
but w prefers m, Wm’ ={w’}

13
CMPT405/705 Foundations Qianping Gu

Analyze algorithm

• Properties of G-S algorithm


(1) Once a w ∈ W is paired, w remains paired.
(2) For pairs (m1 , w) and (m2 , w) with (m1 , w) created earlier than (m2 , w),
gw (m1 ) < gw (m2 ).

• G-S Algorithm terminates after at most n2 iterations of the while loop.

Proof. Let P (t) be the set of pairs (m, w) s.t. m has tried to pair by the end of
2
iteration t of the while loop. Then |P (t + 1)| > |P (t)|. Since there are n pairs in
M × W.

• |P (t)| is a progress measure which specifies that each step of the algorithm
makes the algorithm closer to termination.

14
CMPT405/705 Foundations Qianping Gu

Theorem. [Gale-Shapley 1962] G-S Algorithm returns a stable matching at the


termination.

Proof. Since S is a matching and |M | = |W | = n, by Property (1), if there is an


unpaired m then there is an unpaired w s.t. m has not tried to pair with yet. So, every m
will pair with a w and the algorithm returns a perfect matching S at the termination.

S . Then there is a pair (m, w)


Assume for contradiction that there is an instability w.r.t.
′ ′ ′ ′
s.t. (m, w ), (m , w) ∈ S , fm (w) > fm (w ) and gw (m) > gw (m ). Then m had

tried to pair with w before m paired with w . Since (m, w) 6∈ S , by Property (2),
gw (m) < gw (m′ ), a contradiction. So, S is stable.

15
CMPT405/705 Foundations Qianping Gu

Discussion

• The number of iterations of the while loop can be close to n2 .

• There may be multiple stable matchings for an input instance.

• G-S Algorithm may have different orders of m to make pairs in different


executions.

• For a same input instance, different executions of the algorithm give the same

stable matching S which has the following properties:

– For (m, w ) ∈ S ∗ and (m, w) in any stable matching S ,
fm (w∗ ) ≥ fm (w).

– For (m , w) ∈ S ∗ and (m, w) in any stable matching S ,
gw (m∗ ) ≤ gw (m).

16
CMPT405/705 Foundations Qianping Gu

Computation Models
Word RAM (Random Access Memory)

• Random access memory, input, output and program


• Each memory/input/output cell stores a w-bit integer.
• Primitive operations, arithmetic/logic operations, read/write memory, array
indexing, following a pointer, conditional branch, ...

• Running time, number of primitive operations (constant time per operation).


• Memory size, number of memory cells used.
• More refined models may be needed (e.g., for n-bit integers).
Input
a[i] i
Program Memory

Output

17
CMPT405/705 Foundations Qianping Gu

Turing Machines

• A tape divided into cells, each cell has a symbol from a finite set.
• A set of finite machine states.
• A set of finite instructions which define that given a machine state qi and a
symbol aj , the next step action of the machine.

• When every next step action is unique, it is deterministic Turing machine.


• When some of the next step actions is a set of actions and the machine can take
any one action in the set, it is nondeterministic Turing machine.

• Running time, number of steps.


• Memory size, number of tape cells used.
# 0 1 1 0

#:1
# a b
0:1

18
CMPT405/705 Foundations Qianping Gu

Computational Tractability
• An algorithm is a poly-time (polynomial running time) algorithm if there are
constants c > 0 and d > 0 s.t. for every input of size n, the running time of the
d
algorithm is bounded above by cn primitive computational steps.

• A problem admits a poly-time algorithm if there is an algorithm which, given any


input of size n of the problem, gives an exact solution to the input instance in
polynomial time in n.

• A problem is computational tractable if it admits a poly-time algorithm.

• A poly-time algorithm is called an efficient algorithm.

• A poly-time algorithm has the desirable scaling property: When the input size
doubles, the running time increases by at most a constant factor.

19
CMPT405/705 Foundations Qianping Gu

Measures for running time

• Worst-case running time


Largest running time of an algorithm for any input of size n.

• Average-case running time


Running time of an algorithm averaged for all possible inputs of size n.

The notion of ”averaged” assumes a probability distribution over the inputs.

• Expected running time


Running time of a randomized algorithm.

• Amortized running time


Worst case running time of any sequence of n operations.

20
CMPT405/705 Foundations Qianping Gu

Asymptotic Order of Growth


• A way to describe how the value of a function increases in the limit.

• Foucs on the dominating term for the growth, igonor constant factors and
low-order terms.

• A way to express running times of algorithms.

• A way to compare the growth rates of functions.

• Notations for comparing order of growth

f (n) is O(g(n)) ≈ a≤b


f (n) is Ω(g(n)) ≈ a≥b
f (n) is Θ(g(n)) ≈ a=b
f (n) is o(g(n)) ≈ a<b
f (n) is ω(g(n)) ≈ a>b

21
CMPT405/705 Foundations Qianping Gu

• Big-Oh notation, f (n) is O(g(n)) if there exist constants c > 0 and n0 ≥ 0 s.t.
0 ≤ f (n) ≤ c · g(n) for all n ≥ n0 .
For upper bounds on time/space complexity.

f (n) = 5n2 + 7n + 4, f (n) is O(n2 ); f (n) not O(nk ) for any 0 ≤ k < 2 nor
O(n log n).

22
CMPT405/705 Foundations Qianping Gu

• Big-Omega notation, f (n) is Ω(g(n)) if there exist constants c > 0 and


n0 ≥ 0 s.t. f (n) ≥ c · g(n) ≥ 0 for all n ≥ n0 .
For lower bounds on time/space complexity.

f (n) = 5n2 + 7n + 4, f (n) is Ω(nk ) for any k ≤ 2 and Ω(n log n); f (n) is
k
not Ω(n ) for any k > 2.

23
CMPT405/705 Foundations Qianping Gu

• Big-Theta notation, f (n) is Θ(g(n)) if there exist constants c1 , c2 > 0 and


n0 ≥ 0 s.t. 0 ≤ c1 · g(n) ≤ f (n) ≤ c2 · g(n) for all n ≥ n0 ;
f (n) is Θ(g(n)) iff f (n) is O(g(n)) and Ω(g(n)). For tight (up to a constant
factor) upper and lower bounds on time/space complexity.

f (n) = n2 + 7n + 4, f (n) is Θ(n2 ); f (n) is not Θ(nk ) for k 6= 2.

24
CMPT405/705 Foundations Qianping Gu

• Little-oh notation, f (n) is o(g(n)) if for any constant c > 0, there exists a
constant n0 > 0 s.t. 0 ≤ f (n) < cg(n) for all n ≥ n0 (or
limn→∞ f (n)/g(n) = 0).
f (n) = n2 + 7n + 4 is o(nk ) for k > 2.

• Little-omega notation, f (n) is ω(g(n)) if for any constant c > 0, there exists a
constant n0 > 0 s.t. 0 ≤ cg(n) < f (n) for all n ≥ n0 (or
limn→∞ f (n)/g(n) = ∞).
f (n) = n2 + 7n + 4 is ω(nk ) for k < 2.

25
CMPT405/705 Foundations Qianping Gu

More on Big-Oh notation

• f (n) is O(f (n)).

• If f (n) is O(g(n)) and c > 0 is a constant then c · f (n) is O(g(n)).

• If f (n) is O(f1 (n)) and g(n) is O(g1 (n)), then f (n) · g(n) is
O(f1 (n) · g1 (n)), and f (n) + g(n) is O(max{f1 (n), g1 (n)}).

• If f (n) is O(f1 (n)) and f1 (n) is O(f2 (n)) then f (n) is O(f2 (n)).

• Big-Oh with multiple variables, f (m, n) is O(g(m, n)) if there exist constants
c > 0, m0 ≥ 0 and n0 ≥ 0 s.t. f (m, n) ≤ c · g(m, n) for all m ≥ m0 and
n ≥ n0 .

26
CMPT405/705 Foundations Qianping Gu

Name Running time Example

Constant time O(1) primitive operations

Inverse Ackermann time O(α(n)) some operations in disjoint set data structure

Iterated logarithmic time O(log∗ n)


Log-logarithmic time O(log log n) some operations in a priority queue

Logarithmic time O(log n) binary search in a sorted array

Poly-logarithmic time Poly(log n) AKS primality test on integer n

Fractioner power time O(nc ), 0 < c < 1 factorizing integer n

Linear time O(n) merge two sorted lists into one sorted list

Linearithimic time O(n log n) merge sort

Quadratic time O(n2 ) addition of two n × n matrices

Cubic time O(n3 ) conventional multiplication of two n × n matrices

Polynomial time Poly(n), Poly(log N ) AKS primality test on integer N of n bits


ǫ
Sub-exponential time O(2n ), 0 < ǫ < 1 find maximum independent set in planar graph

Exponential time O(2Poly(n) ) factorizing integer N of n bits (N = Ω(2n ))


Factorial time O(n!) enumerate all permutations of n elements

27
CMPT405/705 Foundations Qianping Gu

AKS primality test: M. Agrawal, N. Kayal and N. Saxena [2002, IIT Kanpur] gave an algorithm
which, given integer N of n bits, decide if N is a prime number in O(nk ) (O((log N )k ))
time, k is a constant.

Factorizing integer: given integer N of n bits, find the prime factors of N . It is open whether
this problem admits an algorithm with poly-time in n or NP-hard. Naive algorithm takes

O( N ) = O(2n/2 ) time, heuristic of O(N 1/4 ) = O(2n/4 ) time is known.

If factorizing integer can be solved in poly-time in n (the number of bits of the integer) then

many public-key encryption schemes including RSA can be cracked easily.

28
CMPT405/705 Foundations Qianping Gu

29
CMPT405/705 Foundations Qianping Gu

Running time of G-S Algorithm

Input M , rank lists of M , W and rank lists of W ; /* O(n2 ) */


:= ∅; Wm = W for every m ∈ M ; /* O(n) */
Initially, S
while ∃ unpaired m ∈ M do /* check condition, O(1); while loop iterates O(n2 ) times */
Choose an unpaired m; Let w ∈ Wm with the largest fm (w); /* O(1) */
if w is unpaired then /* O(1) */
S := S ∪ {(m, w)} /* O(1) */
else
Let (m′ , w) be the pair in S ; /* O(1) */
if rw (m) > rw (m′ ) then /* O(1) */
remove (m′ , w) from S ; S := S ∪ {(m, w)} /* O(1) */
else
Remove w from Wm ; /* O(1) */
end if
end if
end while
Return S ; /* O(n) */
Running time of G-S Alg is O(n2 ).

30
CMPT405/705 Foundations Qianping Gu

Graphs
Undirected graphs

• Graph G(V, E), V is a set of nodes (vertices), E is a set of edges, edge {u, v}
connects nodes u and v and called an edge between u and v .

• Graph size parameters, n = |V |, m = |E|.

• If there are more than one edge between u and v , these edges are called
multiple edges. An edge {u, u} connects node u itself is called a self loop.

• Simple graph, does not have multiple edge nor self loop.

• For an edge e = {u, v}, u, v are the end nodes of e, u is adjacent to v (v is


adjacent to u), u and v are neighbors, e is incident to u and v .

• Node degree deg(u) of node u, number of edges incident to u.

• Graph H is a subgraph of G if V (H) ⊆ V (G) and E(H) ⊆ E(G).

31
CMPT405/705 Foundations Qianping Gu

1 7
V={1,2,3,4,5,6,7,8}
2 3 E={ {1,2},{1,3},{2,3},{2,4},{2,5}
{3,5},{3,7},{3,8},{4,5},{5,6},{7,8} }
4 5 8
n=8, m=11

6
A simple undirected graph

32
CMPT405/705 Foundations Qianping Gu

Graph representation: Adjacency matrix

• For an edge {u, v} in G, u is adjacent to v and v is adjacent to u;


edge {u, v} is incident to u and v .

• Adjacency matrix, an n × n array A with A[u, v] = 1 if u is adjacent to v .

• Space, O(n2 ).

• Time, O(1) to check if {u, v} is an edge; O(n2 ) time to identify all edges.

33
CMPT405/705 Foundations Qianping Gu

Graph representation: Adjacency list

• Adjacency Lists: for every node u of G, all nodes adjacent to u are put in a list.

• Space, O(m + n).

• Time, O(deg(u)) to check if {u, v} is an edge; O(m + n) to identify all edges.

34
CMPT405/705 Foundations Qianping Gu

Path and cycle

• Path in G, a sequence of nodes v1 , v2 , .., vk s.t. each {vi−1 , vi }, 1 < i ≤ k , is


an edge of G.

• Simple path, a path with all nodes distinct.


• Cycle, a path v1 , v2 , .., vk with v1 = vk and v1 , .., vk−1 distinct.
• For graph G, a cycle is defined having at least three edges.
• Length of path (cycle), number of edges in the path (cycle).

1 7

2 3

4 5 8

Path, 1,2,4 6 Cycle, 3,7,8,3

35
CMPT405/705 Foundations Qianping Gu

Trees

• A graph G is connected if for any nodes u, v in G, there is a path between u


and v .

• G is a tree if G is connected and does not have a cycle.

• Rooted tree, a tree with one node identified as the root.

36
CMPT405/705 Foundations Qianping Gu

Graph connectivity and traversal

• s − t connectivity, given nodes s and t, is there a path between s and t.

• Graph traversal, breadth first search (BFS) and depth first search (DFS).

• BFS from a node s:


– L0 = {s}.
– L1 = {all nodes adjacent to s}.
– For i > 1,
Li = {nodes not in L0 ∪ L1 ∪ .. ∪ Li−1 and adjacent to a node in Li−1 }.

• For each i, Li = {nodes with the minimum path length exactly i from s}.

37
CMPT405/705 Foundations Qianping Gu

BFS example

38
CMPT405/705 Foundations Qianping Gu

Bipartite graphs

• Bipartite G, nodes of G can be partitioned into two subsets V1 and V2 s.t. for
every edge e of G, one end node of e is in V1 and the other is in V2 .

• G is bipartite iff G does not have an odd-length cycle.

39
CMPT405/705 Foundations Qianping Gu

Test bipartiteness

• Use BFS on G to compute L0 , .., Lk .


(i) If no edge {u, v} for u, v ∈ Li for any i, G is bipartite.
(ii) If there is an edge {u, v} for u, v in some Li , G is not bipartite.

If G is bipartite, V1 = ∪even i Li and V2 = ∪odd i Li .

40
CMPT405/705 Foundations Qianping Gu

Directed Graphs
• Digraph G(V, E), V is a set of nodes (vertices), E is a set of arcs (directed
edges), arc (u, v) is from node u to node v .

• Graph size parameters, n = |V |, m = |E|.

• If there are more than one arc from u to v , these arcs are called multiple arcs.
An arc (u, u) from u to itself is called a self loop.

• Simple digraph, does not have multiple arcs nor self loop.

• Indegree indeg(u) of node u, number of arcs to u;


outdegree outdeg(u) of node u, number of arcs from u.

indeg(u)=2
u outdeg(u)=3

41
CMPT405/705 Foundations Qianping Gu

1 7
V={1,2,3,4,5,6,7,8}
2 3 A={ (1,2),(3,1),(2,3),(4,2),(2,5)
(3,5),(7,3),(3,8),(5,4),(6,5),(8,7) }
4 5 8
n=8, m=11

6
A simple directed graph

42
CMPT405/705 Foundations Qianping Gu

Adjacency matrix and list for digraphs

1 2 3 4 5 6 7 8 1 2
1 7
1 0 1 0 0 0 0 0 0 2 3 5
2 0 0 1 0 1 0 0 0
2 3 3 1 0 0 0 1 0 0 1 3 1 5 8
4 0 1 0 0 0 0 0 0 4 2
5 0 0 0 1 0 0 0 0 5 4
4 5 8
6 0 0 0 0 1 0 0 0
6 5
7 0 0 1 0 0 0 0 0
8 0 0 0 0 0 0 1 0 7 3
6
8 7
Adjacency matrix Adjacency list

43
CMPT405/705 Foundations Qianping Gu

Directed path and cycle

• Path in digraph G, a sequence of nodes v1 , v2 , .., vk s.t. each (vi−1 , vi ),


1 < i ≤ k , is an arc of G.

• Simple path, a path with all nodes distinct.

• Cycle, a path v1 , v2 , .., vk with v1 = vk and v1 , .., vk−1 distinct.

• Length of path (cycle), number of edges in the path (cycle).

1 7

2 3

4 5 8

Path, 1,2,5,4 6 Cycle, 3,8,7,3

44
CMPT405/705 Foundations Qianping Gu

Digraph search

• Node t is reachable from node s in digraph G if there is a path from s to t.

• Nodes s and t are mutual reachable if each of them is reachable from the other.

• Directed reachability, find all nodes reachable from s.

• G is strongly connected if every pair of nodes is mutually reachable.

• If G is strongly connected can be checked in O(m + n) time: Run BFS from a


node s of G; reverse the orientation of every arc of G to get graph Ḡ; run BFS
from s of Ḡ; return true iff all nodes are reachable from s in both BFS
executions.

u u
s s

v v
G G 1st BFS on G 2nd BFS on G

45
CMPT405/705 Foundations Qianping Gu

Directed acyclic graph (DAG)

• A DAG is a directed graph with no cycle.

• A topological order of directed graph G is an ordering of nodes of G as


v1 , .., vn s.t. for every arc (vi , vj ) of G, i < j .

• G has a topological order iff G is a DAG.

• Given a DAG G, a topological order of G can be found in O(m + n) time.

A topological order

46
CMPT405/705 Foundations Qianping Gu

Topological-Order(G) /*input DAG G, output topological order of G


find a node v of G with indeg(v) = 0;
order v first; delete v from G
Topological-Order(G \ {v}) and append this order after v ;

47
CMPT405/705 Foundations Qianping Gu

Formulate Problems by Graphs


• Given a graph G, a matching of G is a subset M of edges in G s.t. no edges of
M share a common end node.
The stable matching problem can be modelled as a matching problem in
bipartite graphs. This problem can be solved in polynomial time.

• Assume you have n friends, some of your friend may dislike some others, and
you want to invite as many of your friends as possible s.t. no two friends invited
dislike each other.

We can formulate this problem as a problem in graph G: Each node G denotes


a friend and there is an edge between two nodes if they dislike each other. An
independent set S of G is a subset of nodes s.t. there is no edge between any
two nodes in S . The maximum independent set problem is to find an
independent set of maximum size. This problem is NP-complete.

48
CMPT405/705 Foundations Qianping Gu

• Consider a two players game problem: Two companies A and B want to open
similar stores (e.g., provide fast food) in n zones. Each zone has a value of
potential profit for having a store. There are zone regulations that each zone can
have at most one store and if some zone has a store, any zone adjacent to the
zone can not have any store. A and B alternate in choosing a zone to open a
store, starting from A. If each company can reach a predefined profit target?

• The problem is known as Competitive Facility Location problem and can be


formulated as a problem in a graph G: A node of G denotes a zone and is
assigned a positive weight (profit). There is an edge between two nodes if the
two zones are adjacent. A and B alternate in choosing a node of G, starting
from A, to find an independent set of G to maximize the total profit for itself.
This problem is PSPACE-complete.

49
CMPT405/705 Foundations Qianping Gu

Zone 1 Zone 2 Zone 3 Zone 4 Zone 5 Zone 6


profit profit profit profit profit profit
10 5 6 9 7 8

1 2 3 4 5 6
10 5 6 9 7 8

50

You might also like