Download presentation
Presentation is loading. Please wait.
2
233-234233-234 Sedgewick & Wayne (2004); Chazelle (2005) Sedgewick & Wayne (2004); Chazelle (2005) chazelle@cs.princeton.educhazelle@cs.princeton.edu
3
computation theory experimentation
4
computation theory experimentation
5
computation theory experimentation
6
computation theory experimentation Moore’s Law
7
No Moore’s Law there ! there ! No Moore’s Law there ! there ! Inverse Moore’s Law?
8
1. Power of algorithms
9
2. Duality: data & programs 3. Recursion 1. Power of algorithms
10
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)
11
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
12
'; printf ($s,39,$s,39); ?> A program in php
13
$s="$s=%c%s%c;printf$s,34,$s,34;";printf$s,34,$s,34; A program in perl
14
l='l=%s;print l%`l`';print l%`l` A program in python
15
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));}}
16
Why does Schroedinger hate cats? Print next statement twice Output = ? Why does Schroedinger hate cats? A B AB BB
17
Print next statement twice Output = ? Set B=A AA AA Print next statement twice
18
string of symbols datacommand
20
Rogozhin (1996) : 24 states input data output data program data Babbage (1820)
21
Print next statement twice Why does Schroedinger hate cats? Print next statement twice
22
duality recursion
23
duality + recursion Self-reproduction =
24
http://www.accessexcellence.org/RC/VL/GG/dna_replicating.html duality recursion gene - protein dble-stranded
25
What is the difference between classical math and computer science ?
26
Jos Leys
28
Protein Interaction Network Jeong et al
29
Adjacency matrix
30
Adjacency lists
31
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
32
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 vertices with no edges Node
33
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 list add v to w's adjacency list v w x z y
34
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 list add v to w's adjacency list v w x z y
35
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 list add v to w's adjacency list v w x z y
36
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 list add v to w's adjacency list v w x z y w
37
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 list add v to w's adjacency list v w x z y w
38
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 list add v to w's adjacency list v w x z y w
39
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 list add v to w's adjacency list v w z y w x
40
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 d c a b v neighbors adj null a b c d i= 0i= 1 i= 2i= 3 xx
43
1. Birds eat the bread crumbs 2. They don’t random walk DFS/BFS Hansel & Gretel
45
Diffusion equation
46
Normal distribution Random walk
48
With bread crumbs one can find exit in time proportional to V+E DFS/BFS Hansel & Gretel
49
Undirected Depth First Search Adjacency Lists A: F C B G B: A C: A D: F E E: G F D F: A E D: G: E A: H: I: I: H: F A BCG DE HI
50
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack F newly discovered
51
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack visit(F) (F, A) (F, E) (F, D) A already marked
52
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack visit(F) (F, A) (F, E) (F, D) E newly discovered E
53
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack visit(F) (F, A) (F, E) (F, D) visit(E) (E, G) (E, F) (E, D) G newly discovered G
54
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack visit(F) (F, A) (F, E) (F, D) visit(E) (E, G) (E, F) (E, D) visit(G) (G, E) (G, A) E already marked
55
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack visit(F) (F, A) (F, E) (F, D) visit(E) (E, G) (E, F) (E, D) visit(G) (G, E) (G, A) A already marked
56
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI 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
57
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack visit(F) (F, A) (F, E) (F, D) visit(E) (E, G) (E, F) (E, D) F already marked
58
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack visit(F) (F, A) (F, E) (F, D) visit(E) (E, G) (E, F) (E, D) D newly discovered D
59
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack visit(F) (F, A) (F, E) (F, D) visit(E) (E, G) (E, F) (E, D) visit(D) (D, F) (D, E) F already marked
60
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack visit(F) (F, A) (F, E) (F, D) visit(E) (E, G) (E, F) (E, D) visit(D) (D, F) (D, E) E already marked
61
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI 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
62
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack visit(F) (F, A) (F, E) (F, D) visit(E) (E, G) (E, F) (E, D) Finished E
63
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack visit(F) (F, A) (F, E) (F, D) D already marked
64
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack visit(F) (F, A) (F, E) (F, D) Finished F
65
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack C newly discovered C
66
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack visit(C) (C, A) A already marked
67
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack visit(C) (C, A) Finished C
68
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack B newly discovered B
69
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack visit(B) (B, A) A already marked
70
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack visit(B) (B, A) Finished B
71
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack G already finished
72
visit(A) (A, F) (A, C) (A, B) (A, G) Undirected Depth First Search F A BCG DE HI Stack Finished A
73
Undirected Depth First Search F A BCG DE HI Stack
74
Undirected Depth First Search F A BCG DE HI Stack visit(H) (H, I) I newly discovered I
75
Undirected Depth First Search F A BCG DE HI Stack visit(H) (H, I) visit(I) (I, H) H already marked
76
Undirected Depth First Search F A BCG DE HI Stack visit(H) (H, I) visit(I) (I, H) Finished I
77
Undirected Depth First Search F A BCG DE HI Stack visit(H) (H, I) FinishedH
78
Undirected Depth First Search F A BCG DE HI Stack
79
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.
80
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 b a c d w 0
81
Is graph connected?
82
Depth First Search
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.