Lec3 Handout
Lec3 Handout
13 February 2022
Algorithm bar
Algorithm foo for i = 1 to n do
for i = 1 to n do for j = 1 to n do
for j = 1 to n do for k = 1 to n do
constant-time op constant-time op
end for end for
end for end for
end for
▶ f is Θ(f )
Example:
n2 + 42n + n log n is Θ(n2 )
|{z} | {z }
g f
Examples:
n = o(n2 )
log n = o(n)
nk = o(2n )
▶ Examples
These are polynomial time:
f1 (n) = n
Not polynomial time:
f2 (n) = 4n + 100
f7 (n) = 2n
f3 (n) = n log(n) + 2n + 20
f8 (n) = 3n
f4 (n) = 0.01n2
f9 (n) = n!
f5 (n) = n2
f6 (n) = 20n2 + 2n + 3
Why Polynomial Time ?
k
An algorithm is exponential time if it is O(2n ) for some k > 0
equivalently: O(2poly(n) ). Does the base 2 matter?
Pn
H(n) = 1 + 1/2 + ... + 1/n = k=1 1/k
▶ Transportation networks:
hubs, links, routes
shortest paths
▶ Communication networks:
routing, hops,
latency/throughput?
connectivity, spanning trees
▶ Information networks:
WWW, what are important/authoritative pages?
Figure 1. Largest Connected Subcomponent of the Social Network in the Framingham Heart Study in the Year 2000.
Each circle (node) represents one person in the data set. There are 2200 persons in this subcomponent of the social
network. Circles with red borders denote women, and circles with blue borders denote men. The size of each circle
is proportional to the person’s body-mass index. The interior color of the circles indicates the person’s obesity status:
yellow denotes an obese person (body-mass index, ≥30) and green denotes a nonobese person. The colors of the
ties between the nodes indicate the relationship between them: purple denotes a friendship or marital tie and orange
denotes a familial tie.
“The Spread of Obesity in a Large Social Network over 32 Years” by Christakis and Fowler in New England Journal of Medicine, 2007 6
The Political Blogosphere and the 2004 U.S. Election: Divided They Blog, Adamic and Glance, 2005
Figure 1: Community structure of political blogs (expanded set), shown using utilizing a GEM
layout [11] in the GUESS[3] visualization and analysis tool. The colors reflect political orientation, 37
red for conservative, and blue for liberal. Orange links go from liberal to conservative, and purple
slide credit: Kevin Wayne / Pearson
More applications
▶ Network science
▶ random graphs: various evolution models
▶ scale-free, small world
▶ Analyzing programs
▶ control flow graph, function call graph
▶ state space search (also in games):
compute reachable states (configurations)
is an error state reachable?
Graphs
MIT CASE
BBN CARN
Figure
A path2.3: is
Anaalternate
sequencedrawing
P =ofvthe
1 , v13-node Internet
2, . . . , v k−1 , vkgraph
suchfrom December
that each 1970.
consecutive pair vi , vi+1 is joined by an edge in G
Paths. Although
Called: pathwe’ve
“frombeen
v1discussing
to vk ”. examples
Or: a v1of–vgraphs
k pathin many di↵erent areas, there
are clearly some common themes in the use of graphs across these areas. Perhaps foremost
Special
among these iscase: empty
the idea that path
things from any vacross
often travel to itself (justofnode,
the edges nomoving
a graph, edges)from
Simple Path, Distance, Cycle
▶ Distance from u to v:
minimum number of edges in a u–v path
2 3
Node set: {1, 2, 4}: there is a path between any two nodes
But it is not maximal: can add 3, property still holds
{1, 2, 3, 4} is maximal: can’t add any other node
Trees
Tree properties
Let G be an undirected graph with n nodes.
Then any two of the following statements imply the third:
▶ G is connected
▶ G does not contain a cycle
▶ G has n − 1 edges
1 4 5
2 3
you
Layers
▶ L0 = {s}
▶ L1 = nodes with edge to L0
▶ L2 = nodes with an edge to L1 that don’t belong to L0 or L1
▶ ...
▶ Li+1 = nodes with an edge to Li that don’t belong to any
earlier layer.
Observation:
There is a path from s to t if and only if t appears in some layer.
BFS Implementation
BFS(s):
mark s as "discovered"
L[0] ← {s}, i ← 0 ▷ Discover s
while L[i] is not empty do
L[i + 1] ← empty list
for all nodes v in L[i] do
for all neighbors w of v do ▷ Explore v
if w is not marked "discovered" then
mark w as "discovered" ▷ Discover w
put w in L[i + 1]
end if
end for
end for
i←i+1
end while
HARV CASE
SRI SDC RAND