Logistics HW 2 due now HW 3 out this afternoon Reading: K&T - - PowerPoint PPT Presentation

logistics
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Logistics

HW 2 due now HW 3 out this afternoon Reading: K&T 3.4-3.6 Questions?

slide-2
SLIDE 2

Logistics

Homework: 35% Midterms: 30% Final: 20% Quizzes + participation: 15%

slide-3
SLIDE 3

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....

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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)

slide-8
SLIDE 8

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

slide-9
SLIDE 9

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)

slide-10
SLIDE 10

Graphs Chapter 3

slide-11
SLIDE 11

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

slide-12
SLIDE 12

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…

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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

slide-15
SLIDE 15

Definitions

slide-16
SLIDE 16

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

slide-17
SLIDE 17

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

slide-18
SLIDE 18

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

slide-19
SLIDE 19

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

slide-20
SLIDE 20

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.

slide-21
SLIDE 21

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

slide-22
SLIDE 22

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?

slide-23
SLIDE 23

Review Definitions

What to know: n, m, neighbor, incident, path, distance, cycle, connected, tree Example on board

slide-24
SLIDE 24

Graph Traversal

Is a graph connected?

1 3 5 4 2

easy hmmm...

slide-25
SLIDE 25

Graph Traversal

Is a graph connected? Approach: explore outward from arbitrary starting node s to find all nodes reachable from s (connected component)

slide-26
SLIDE 26

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:

slide-27
SLIDE 27

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.

slide-28
SLIDE 28

BFS Tree

If we keep only the edges traversed while doing a breadth-first search, we will have a tree.

Example on board

slide-29
SLIDE 29

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

slide-30
SLIDE 30

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

slide-31
SLIDE 31

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

slide-32
SLIDE 32

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); } } }

slide-33
SLIDE 33

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

slide-34
SLIDE 34

Summary

Definitions: G = (V , E), n = |V|, m = |E|, neighbor, incident, cycle, path, connected, tree BFS and DFS: two ways to traverse a graph; each produces a tree BFS: short, broad tree (“bushy”) DFS: deep, narrow tree (“scraggly”)