Download presentation

Presentation is loading. Please wait.

Published byMadlyn Farmer Modified over 2 years ago

1
Sociology and CS Philip Chan

2
How close are people connected? Are people closely connected, not closely connected, isolated into groups, …

3
Degree of Separation The number of connections to reach another person

4
Milgram’s Experiment Stanley Milgram, psychologist Experiment in the late 1960’s Chain letter to gather data Stockbroker in Boston 160 people in Omaha, Nebraska Given a packet Add name and forward it to another person who might be closer to the stockbroker Partial “social network”

5
Small World Six degrees of separation Everyone is connected to everyone by a few people—about 6 on the average. Obama might be 6 connections away from you “Small world” phenomenon

6
Bacon Number Number of connections to reach actor Kevin Bacon http://oracleofbacon.org/ Is a connection in this network different from the one in Milgram’s experiment?

7
Problem Formulation Given (input) People Connections/links/friendships Find (output) the average number of connections between two people Simplification don’t care about how long/strong/… the friendships are

8
Problem Formulation Formulate it into a graph problem (abstraction) Given (input) People Connections Find (output) the average number of connections between two people

9
Problem Formulation Formulate it into a graph problem (abstraction) Given (input) People -> vertices Connections -> edges Find (output) the average number of connections between two people -> ?

10
Problem Formulation Formulate it into a graph problem (abstraction) Given (input) People -> vertices Connections -> edges Find (output) the average number of connections between two people -> average shortest path length

11
Algorithm Shortest Path Dijkstra’s algorithm Limitations?

12
Algorithm Shortest Path Dijkstra’s algorithm This could be an overkill, why?

13
Algorithm Unweighted edges Each edge has the same weight of 1 Simpler algorithm? Any ideas?

14
Breadth-First Search Start from the origin Explore people one link away (dist=1) Explore people two links away (dist=2) … Until the destination is reach

15
Algorithm Breadth-first search (BFS) Single origin, all destinations

16
Algorithm Breadth-first search (BFS) Single origin, all destinations Repeat BFS for each origin ShortestDistance(x,y) = shortestDistance(y,x)

17
Algorithm Breadth-first search (BFS) Single origin, all destinations Repeat BFS for each origin ShortestDistance(x,y) = shortestDistance(y,x) Each subsequent BFS can stop earlier

18
BFS Algorithm—A Closer Look Add origin to the open list While open list is not empty 1. Remove the first person from the open list 2. Mark the person as visited 3. Add non-visited neighbors of the person to the open list

19
BFS Algorithm—A Closer Look Add (origin, 0) to the open list While open list is not empty 1. Remove the first item (person, dist) from the open list 2. Mark person as visited 3. For each non-visited neighbor of person Add (neighbor, dist + 1) to the open list

20
Implementation Data Structures What do we need to keep track of? Whether we have visited some person before An ordered list of persons to be visited Implementation Boolean visited[person] Open list of to be visited people Queue First in first out (FIFO)

21
Queue Operations Enqueue Add an item at the end Dequeue Remove an item from the front

22
Queue Implementation 1 Consider using an array How to implement the two operations? Enqueue Update tail, add item at tail Dequeue

23
Queue Implementation 1 Consider using an array How to implement the two operations? Enqueue Update tail, add item at tail Dequeue Remove the first item Shift the rest of the items up, update tail

24
Additional Operations What do we need to check before Enqueue? What do we need to check before Dequeue?

25
Additional Operations What do we need to check before Enqueue? What do we need to check before Dequeue? Enqueue: Is the queue full? Dequeue Is the queue empty?

26
Queue Implementation 1(shifting items) Is the queue empty?

27
Queue Implementation 1(shifting items) Is the queue empty? When tail is -1 Is the queue full?

28
Queue Implementation 1(shifting items) Is the queue empty? When tail is -1 Is the queue full? When tail is length - 1

29
Number of assignments (Speed of algorithm) N = number of items on the queue Count number of assignments involving items in the queue

30
Number of assignments (Implementation 1) Enqueue

31
Number of assignments (Implementation 1) Enqueue 1 (assign new item at tail) Dequeue

32
Number of assignments (Implementation 1) Enqueue 1 (assign new item at tail) Dequeue 1 (assign first item to somewhere else) N-1 shifts (assignments) Total N

33
Shifting items in Dequeue Quite a bit of work if we have many items in the queue Can we avoid shifting? If so, how? Ideas?

34
Queue Implementation 2 Consider using an array How to implement the two operations? Enqueue Head and tail Update tail, add at tail Dequeue

35
Queue Implementation 2 Consider using an array How to implement the two operations? Enqueue Head and tail update tail, add at tail Dequeue Get item from head Update head

36
Queue Implementation 2 (head and tail) Is the queue empty?

37
Queue Implementation 2 (head and tail) Is the queue empty? When head is larger than tail Is the queue full?

38
Queue Implementation 2 (head and tail) Is the queue empty? When head is larger than tail Is the queue full? When tail is length - 1

39
Number of assignments (Implementation 2) Enqueue

40
Number of assignments (Implementation 2) Enqueue 1 (assign new item at tail) Dequeue

41
Number of assignments (Implementation 2) Enqueue 1 (assign new item at tail) Dequeue 1 (assign item at tail to somewhere else)

42
What if tail reaches the end of array But there is room in the front of the array? Any ideas to reuse the space in the front?

43
Queue Implementation 3 Circular queue Wrap around If we use head and tail (Implementation 2) What needs to be changed?

44
Circular Queue Operations Length = length of array Enqueue

45
Circular Queue Operations Length = length of array Enqueue If not full If tail < length increment tail Else tail = 0 Add item Dequeue

46
Circular Queue Operations Length = length of array Enqueue If not full If tail < length increment tail Else tail = 0 Add item Dequeue Similarly for head

47
Queue Implementation 3 (circular queue) Is the queue empty?

48
Queue Implementation 3 (circular queue) Is the queue empty? When head is larger than tail With wrap-around adjustment Is the queue full?

49
Queue Implementation 3 (circular queue) Is the queue empty? When head is larger than tail With wrap-around adjustment Is the queue full? When head is larger than tail With wrap-around adjustment Same!! How can we distinguish them?

50
Queue Implementation 3 (circular queue) Is the queue empty? When head is larger than tail With wrap-around adjustment Is the queue full? When head is larger than tail With wrap-around adjustment Same!! How can we distinguish them? Keep track of size instead

51
Keeping track of queue size Enqueue: increment size Dequeue: decrement size

52
Summary Understand how people are connected Degrees of separation shortest distance between two people Average degree of separation (community) Sum of shortest distance of all pairs / # of pairs Algorithm for shortest distance (distance 1 in all edges) Breadth-first search (BFS) Different origins BFS Open list: people to be explored Queue Circular queue (implementation 3)

Similar presentations

OK

CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.

CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google