233-234233-234 sedgewick & wayne (2004); chazelle (2005) sedgewick & wayne (2004); chazelle...
Post on 20-Dec-2015
219 views
TRANSCRIPT
233-234233-234233-234233-234
Sedgewick & Wayne (2004); Chazelle (2005)Sedgewick & Wayne (2004); Chazelle (2005) Sedgewick & Wayne (2004); Chazelle (2005)Sedgewick & Wayne (2004); Chazelle (2005)
[email protected]@[email protected]@cs.princeton.edu
computation
theory experimentation
computation
theory experimentation
computation
theory experimentation
computation
theoryexperimenta
tion
Moore’s LawMoore’s Law
No Moore’s Law No Moore’s Law there !there !No Moore’s Law No Moore’s Law there !there !
Inverse Moore’s Inverse Moore’s Law?Law?Inverse Moore’s Inverse Moore’s Law?Law?
1. Power of algorithms
2. Duality: data & programs
3. Recursion
1. Power of algorithms
A program in C
/**/char q='"',*a="*//**/char q='%c',*a=%c%s%c*/};)b(stup;]d[b=]d-472[b)--d(elihw;)q,a,q,q,2+a,b(ftnirps;)b(stup{)(niam;731=d tni;]572[b,",b[275];int d=137;main(){puts(b);sprintf(b,a+2,q,q,a,q);while(d--)b[274-d]=b[d];puts(b);}/*c%s%c%=a*,'c%'=q rahc/**//*"=a*,'"'=q rahc/**/
(By Dan Hoey)
char*s="char*s=%c%s%c;void main(){printf(s,34,s,34);}";void main(){printf(s,34,s,34);}
A program in C
<?php $s='<?php $s=%c%s%c; printf($s,39,$s,39); ?>'; printf ($s,39,$s,39); ?>
A program in php
$s="$s=%c%s%c;printf$s,34,$s,34;";printf$s,34,$s,34;
A program in perl
l='l=%s;print l%%`l`';print l%`l`
A program in python
class S{public static void main(String[]a){String s="class S{public static void main(String[]a){String s=;char c=34;System.out.println(s.substring(0,52)+c+s+c+s.substring(52));}}";char c=34;System.out.println(s.substring(0,52)+c+s+c+s.substring(52));}}
A program in Java
Output = ?
class S{public static void main(String[]a){String s="class S{public static void main(String[]a){String s=;char c=34;System.out.println(s.substring(0,52)+c+s+c+s.substring(52));}}";char c=34;System.out.println(s.substring(0,52)+c+s+c+s.substring(52));}}
Why does Schroedinger hate cats?
Print next statement twice
Output = ?
Why does Schroedinger hate cats?
Why does Schroedinger hate cats?
A
B
AB BB
Print next statement twice
Output = ?
Set B=A
AA AA
Print next statement twice
Print next statement twice
Print next statement twice
string of symbols
data command
Rogozhin (1996) : 24 states
input data output data
program
data
Babbage (1820)
Print next statement twice
Print next statement twice
Why does Schroedinger hate cats?
Print next statement twice
Print next statement twice
Print next statement twice
duality
recursion
duality + recursion
Self-reproduction
=
http://www.accessexcellence.org/RC/VL/GG/dna_replicating.html
duality
recursion
gene - protein
dble-stranded
What is the difference between classical math and computer science ?
Jos Leys
Protein Interaction Network
Jeong et al
Adjacency matrix
Adjacency lists
public class Graph { private int V; private Node[] adj; private int[] degree;
private static class Node { int vertex; Node next; Node(int v, Node next) { this.vertex = v; this.next = next; } }
Adjacency List Implementation
vertex next
Node
public class Graph { private int V; private Node[] adj; private int[] degree;
private static class Node { int vertex; Node next; Node(int v, Node next) { this.vertex = v; this.next = next; } }
public Graph(int V) { this.V = V; adj = new Node[V]; degree = new int[V]; }
Adjacency List Implementation
graph on V verticeswith no edges
Node
NodeNodeNode
NodeNode
public int V() { return V; }
public void addEdge(int v, int w) { adj[v] = new Node(w, adj[v]); adj[w] = new Node(v, adj[w]); degree[v]++; degree[w]++;}
Adjacency List Implementation
# vertices
add w to v's adjacency listadd v to w's adjacency list
v
w
x zy
public int V() { return V; }
public void addEdge(int v, int w) { adj[v] = new Node(w, adj[v]); adj[w] = new Node(v, adj[w]); degree[v]++; degree[w]++;}
Adjacency List Implementation
# vertices
add w to v's adjacency listadd v to w's adjacency list
v
w
x zy
public int V() { return V; }
public void addEdge(int v, int w) { adj[v] = new Node(w, adj[v]); adj[w] = new Node(v, adj[w]); degree[v]++; degree[w]++;}
Adjacency List Implementation
# vertices
add w to v's adjacency listadd v to w's adjacency list
v
w
x zy
public int V() { return V; }
public void addEdge(int v, int w) { adj[v] = new Node(w, adj[v]); adj[w] = new Node(v, adj[w]); degree[v]++; degree[w]++;}
Adjacency List Implementation
# vertices
add w to v's adjacency listadd v to w's adjacency list
v
w
x zy
w
public int V() { return V; }
public void addEdge(int v, int w) { adj[v] = new Node(w, adj[v]); adj[w] = new Node(v, adj[w]); degree[v]++; degree[w]++;}
Adjacency List Implementation
# vertices
add w to v's adjacency listadd v to w's adjacency list
v
w
x zy
w
public int V() { return V; }
public void addEdge(int v, int w) { adj[v] = new Node(w, adj[v]); adj[w] = new Node(v, adj[w]); degree[v]++; degree[w]++;}
Adjacency List Implementation
# vertices
add w to v's adjacency listadd v to w's adjacency list
v
w
x zy
w
public int V() { return V; }
public void addEdge(int v, int w) { adj[v] = new Node(w, adj[v]); adj[w] = new Node(v, adj[w]); degree[v]++; degree[w]++;}
Adjacency List Implementation
# vertices
add w to v's adjacency listadd v to w's adjacency list
v
w
zyw x
public int[] neighbors(int v) { int[] neighbors = new int[degree[v]]; int i = 0; for (Node x = adj[v]; x != null; x = x.next) neighbors[i++] = x.vertex; return neighbors;}
Return list of neighbors of v as an array
dca bv
neighborsadj
null
a b c d
i= 0 i= 1 i= 2 i= 3
x x
1. Birds eat the bread crumbs
2. They don’t
random walk
DFS/BFS
Hansel & Gretel
Diffusion equation
Diffusion equation
Normal distribution
Random walk
With bread crumbs one canfind exit in time proportional
to V+E DFS/BFS
Hansel & Gretel
Undirected Depth First Search
Adjacency Lists
A: F C B GB: AC: AD: F EE: G F DF: A E D:G: E A:H: I:I: H:
F
A
B C G
D E
H I
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
F newly discovered
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(F)
(F, A) (F, E) (F, D)
A alreadymarked
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(F)
(F, A) (F, E) (F, D)
E newly discovered
E
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(F)
(F, A) (F, E) (F, D)
visit(E)
(E, G) (E, F) (E, D)
G newly discovered
G
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(F)
(F, A) (F, E) (F, D)
visit(E)
(E, G) (E, F) (E, D)
visit(G)
(G, E) (G, A)
E alreadymarked
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(F)
(F, A) (F, E) (F, D)
visit(E)
(E, G) (E, F) (E, D)
visit(G)
(G, E) (G, A)
A alreadymarked
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(F)
(F, A) (F, E) (F, D)
visit(E)
(E, G) (E, F) (E, D)
visit(G)
(G, E) (G, A)
Finished G
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(F)
(F, A) (F, E) (F, D)
visit(E)
(E, G) (E, F) (E, D)
F alreadymarked
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(F)
(F, A) (F, E) (F, D)
visit(E)
(E, G) (E, F) (E, D)
D newly discovered
D
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(F)
(F, A) (F, E) (F, D)
visit(E)
(E, G) (E, F) (E, D)
visit(D)
(D, F) (D, E)
F alreadymarked
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(F)
(F, A) (F, E) (F, D)
visit(E)
(E, G) (E, F) (E, D)
visit(D)
(D, F) (D, E)
E alreadymarked
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(F)
(F, A) (F, E) (F, D)
visit(E)
(E, G) (E, F) (E, D)
visit(D)
(D, F) (D, E)
Finished D
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(F)
(F, A) (F, E) (F, D)
visit(E)
(E, G) (E, F) (E, D)
Finished E
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(F)
(F, A) (F, E) (F, D)
D alreadymarked
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(F)
(F, A) (F, E) (F, D)
Finished F
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
C newly discovered
C
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(C)
(C, A)
A alreadymarked
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(C)
(C, A)
Finished C
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
B newly discovered
B
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(B)
(B, A)
A alreadymarked
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(B)
(B, A)
Finished B
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
G alreadyfinished
visit(A)
(A, F) (A, C) (A, B) (A, G)
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
Finished A
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(H)
(H, I)
I newly discovered
I
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(H)
(H, I)
visit(I)
(I, H)
H alreadymarked
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(H)
(H, I)
visit(I)
(I, H)
Finished I
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
visit(H)
(H, I)
FinishedH
Undirected Depth First Search
F
A
B C G
D E
H I
Stack
public class DFSearcher { private static int UNMARKED = -1; private int[] cc; private int components;
public DFSearcher(Graph G) { // next slide }
public int component(int v) { return cc[v]; } public int components() { return components; }
Connected components of a graph G.
* For each vertex, give id number corresponding to component.
public DFSearcher(Graph G) { components = 0; cc = new int[G.V()]; for (int v = 0; v < G.V(); v++) cc[v] = UNMARKED; for (int v = 0; v < G.V(); v++) { if (cc[v] == UNMARKED) { dfs(G, v); components++; } } }
private void dfs(Graph G, int v) { cc[v] = components; int[] neighbors = G.neighbors(v); for (int i = 0; i < neighbors.length; i++) { int w = neighbors[i]; if (cc[w] == UNMARKED) dfs(G, w); } }
v
c
b
d
a
ba c dw
0
Is graph connected?
Depth First Search