Logistics HW 2 due now HW 3 out this afternoon Reading: K&T - - PowerPoint PPT Presentation
Logistics HW 2 due now HW 3 out this afternoon Reading: K&T - - PowerPoint PPT Presentation
Logistics HW 2 due now HW 3 out this afternoon Reading: K&T 3.4-3.6 Questions? Logistics Homework: 35% Midterms: 30% Final: 20% Quizzes + participation: 15% Some Comments I look forward to learning more about the p versus np problem
Logistics
Homework: 35% Midterms: 30% Final: 20% Quizzes + participation: 15%
Some Comments
I look forward to learning more about the p versus np problem and learning about what problems cannot be efficiently solved. The course text book - Algorithms Design - is really fun to read. It explains concepts in a very clear way. I realized how much I forgot from Data Structure :( The course seems ok so far... I think....
Some Questions
Will we usually get graded homework back on the same day that new homework is due? I would appreciate it if you could explain some of the implementations of data structures for Gale Shapley algorithm (how many lists, arrays in the first implementation, etc). sorting with priority queues
Running Time?
Initialize each college and student to be free. while (some college is free and hasn't made offers to every student) { Choose such a college c s = 1st student on c’s list to whom c has not made offer if (s is free) assign c and s to be engaged else if (s prefers c to current college c’) assign c and s to be engaged, and c’ to be free else s rejects c }
O(n2) if constant time inside the loop
About Data Structures...
For our purposes, most data structures are “black boxes” with certain running-time guarantees Good news: don’ t need to remember details Bad news: they may seem opaque if you don’ t remember details
Lists and Arrays
Array List Get ith entry find element e insert/delete
O(1) O(i) O(n) O(log n) if sorted O(n) O(n) O(1)
Which Data Structures Should We Use?
Need to do following in O(1) time Find free college c Find next student s in preference list of c Find current college c’ of s Check if s likes c’ better then c
linked list
freeColleges
two arrays
Pref[c,Next[c]]
array
Current[s]
array
Rank[s,c]
Example on board
Another Example: Heapsort
Input: unsorted array A[] Let Q be a heap-based priority queue for i = 1 to n Insert(Q, A[i]) end for i = 1 to n A[i] = ExtractMin(Q) end
(n x Insert) + (n x ExtractMin) → O(n log n) if Insert and ExtractMin are O(log n)
Graphs Chapter 3
Undirected Graph
Undirected graph. G = (V , E) V = nodes (vertices) E = edges between pairs of nodes. Captures pairwise relationship between
- bjects.
Graph size parameters: n = |V|, m = |E|. V = {1, 2, 3, 4, 5} E = {(1,2), (1,4), (1,5), (2,3), (2,4), (3,5)} n=5 m=6
1 3 5 4 2
Graphs are Ubiquitous
Google Maps: What is the shortest driving route from Amherst to Florida? Facebook: how many “degrees of separation” between me and Barack Obama? Many more…
Four Degrees of Separation
Lars Backstrom∗ Paolo Boldi† Marco Rosa† Johan Ugander∗ Sebastiano Vigna† January 6, 2012
Abstract
Frigyes Karinthy, in his 1929 short story “Láncszemek” (“Chains”) suggested that any two persons are distanced by at most six friendship links.1 Stanley Milgram in his famous experiment [20, 23] challenged people to route postcards to a fixed recipient by passing them only through direct acquain-
- tances. The average number of intermediaries on the path
- f the postcards lay between 4.4 and 5.7, depending on the
sample of people chosen. We report the results of the first world-scale social-network graph-distance computations, using the entire Facebook net- work of active users (≈ 721 million users, ≈ 69 billion friend- ship links). The average distance we observe is 4.74, cor- responding to 3.74 intermediaries or “degrees of separation”, showing that the world is even smaller than we expected, and prompting the title of this paper. More generally, we study the distance distribution of Facebook and of some interest- ing geographic subgraphs, looking also at their evolution over time. The networks we are able to explore are almost two orders
arXiv:1111.4570v3 [cs.SI] 5 Jan 2012
Habitat Accessibility: 0 1.0
(a) budget = 0, z = 5.4 × 105 (b) budget = 1000, z = 2.2 × 106 (c) budget = 10000, z = 5.8 × 106
Figure 4: Visualization of the barrier removal policies
Fish, Rivers and Trees
Definitions
Terminology
If e = (u, v) is an edge, then: (1) u is a neighbor of v (2) u is adjacent to v (3) e is incident on u and v (4) u and v are the endpoints of e
1 3 5 4 2
1 is adjacent to 2 (1,2) is incident on 1 and 2
1 3 5 4 2
Path
A path is a sequence P of nodes v1, v2, …, vk-1, vk with the property that each consecutive pair vi, vi+1 is joined by an edge in E.
1 3 5 4 2
1-4-2 is a path. 1-3-4 is NOT a path.
1 3 5 4 2
Distance
The distance from u to v is the minimum number of edges in any path from u to v
1 3 5 4 2
distance(1,2) = 1 distance(1,3) = 2
Cycle
A cycle is a path v1, v2, …, vk-1, vk in which v1 = vk, k > 2, and the first k-1 nodes are all distinct.
1-2-4-1 is a cycle. 1-2-4 is NOT a cycle. 1-2-4-1-5 is NOT a cycle. 1-2-4-1-5-3-2-1 is NOT a cycle.
1 3 5 4 2
Connectivity
An undirected graph is connected if for every pair of nodes u and v, there is a path between u and v.
1 3 5 4 2
is a connected graph.
1 3 5 4 2
is NOT a connected graph.
Trees
A tree is an undirected graph that is connected and does not contain a cycle.
1 3 5 4 2
is NOT a tree
1 3 5 4 2
is a tree
Trees
1 3 5 4 2 1 3 5 4 2 1 3 5 4 2
http:/ /www.offbeattravel.com/MoCA.html
(Upside-down)
Parents, descendants, ancestors?
Review Definitions
What to know: n, m, neighbor, incident, path, distance, cycle, connected, tree Example on board
Graph Traversal
Is a graph connected?
1 3 5 4 2
easy hmmm...
Graph Traversal
Is a graph connected? Approach: explore outward from arbitrary starting node s to find all nodes reachable from s (connected component)
Is a Graph Connected?
Algorithm 1: Breadth-first search (BFS) Explore outward by distance
a e b d c
Start at a:
a e b d c
Visit all nodes at distance 1 from a:
a e b d c
Visit all nodes at distance 2 from a:
Breadth-First Search
Layers L0 = { s }. L1 = all neighbors of L0 L2 = nodes with edge to L1 that do not belong to L0 or L1
...
Li+1 = nodes with edge to Li that do not belong to an earlier layer Li+1 = { v: ∃ (u,v) ∈ E, u ∈ Li, v ∉ L0 ∪ … ∪ Li } Observation: Li consists of all nodes at distance exactly i from s. There is a path from s to t iff t appears in some layer.
BFS Tree
If we keep only the edges traversed while doing a breadth-first search, we will have a tree.
Example on board
BFS Tree
- Property. Let T be a BFS tree of G = (V
, E), and let (x, y) be an edge of G. Then the layer of x and y differ by at most 1.
a e b d c
Layer 0: {a} Layer 1: {b, c, d} Layer 2: {e}
Proof on board
A More General Strategy
To explore the connected component, add any node v for which: (u, v) is an edge u is explored, but v is not Picture on board
Is a Graph Connected?
Algorithm 2: Depth-first search (DFS) - Keep exploring from most recently added node until you have to backtrack
a e b d c a e b d c a e b d c a e b d c a e b d c
DFS Algorithm
DFS(u) { mark u as “explored” for each edge (u,v) incident to u { if (v is not marked as “explored”) { DFS(v); } } }
Depth First Search
Theorem: Let T be a depth-first search tree. Let x and y be 2 nodes in the tree. Let (x, y) be an edge that is in G but not in T. Then either x is an ancestor of y or y is an ancestor
- f x in T.
Proof?
a e b d c a e b d c