Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
29 views64 pages

Dsa L07

I want to find the fewest steps to Vicki. 13 / 21 Example: social network How to find the fewest steps to someone on a social network? Idea: • Me? • My friends? • Friends’ friends? • Friends’ friends’ friends? • ... John Helena Paul Donald Jared Chris Vicki

Uploaded by

ruoningzhu7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views64 pages

Dsa L07

I want to find the fewest steps to Vicki. 13 / 21 Example: social network How to find the fewest steps to someone on a social network? Idea: • Me? • My friends? • Friends’ friends? • Friends’ friends’ friends? • ... John Helena Paul Donald Jared Chris Vicki

Uploaded by

ruoningzhu7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 64

Data Structures and

Algorithms
Live Class 7

Heikki Peura
[email protected]
Announcements

Homework 3 is on the Hub


• Algorithms, libraries, . . .
• Complete in your assigned study groups
Today

1. Representing networks as graphs

2. Graph search

2 / 21
Graph problems

Suppose you’re travelling around Europe


• Know flights and their prices
• Goal: fewest stops between cities, cheapest price, ...?

3 / 21
Defining graphs
70

30 50
OSL ARN HEL
80
120
50
190

110
LON AMS RIX
40

90

A set of nodes (or vertices) connected by edges

4 / 21
Defining graphs
70

30 50
OSL ARN HEL
80
120
50
190

110
LON AMS RIX
40

90

A set of nodes (or vertices) connected by edges

Edges may or may not be directed → directed or undirected graph


(Twitter/X vs Facebook?)

4 / 21
Defining graphs
70

30 50
OSL ARN HEL
80
120
50
190

110
LON AMS RIX
40

90

A set of nodes (or vertices) connected by edges

Edges may or may not be directed → directed or undirected graph


(Twitter/X vs Facebook?)

Edges may have weights (eg prices): weighted graph


4 / 21
Pics: Gmaps, Uber, Hautsch et al.
Graphs are everywhere

Represent situations with pairwise relationships

Many applications, some not obvious:


• Roads, social networks, the web, financial markets,
complex projects etc

6 / 21
Graph: collection of vertices and edges
Graph G = (V , E): V = set of n vertices, E = set of m edges
• Edges can be directed or undirected
• Connected graph: there is a path between any two vertices

7 / 21
Graph: collection of vertices and edges
Graph G = (V , E): V = set of n vertices, E = set of m edges
• Edges can be directed or undirected
• Connected graph: there is a path between any two vertices

How many edges can there be in a connected graph with n vertices?

7 / 21
Graph: collection of vertices and edges
Graph G = (V , E): V = set of n vertices, E = set of m edges
• Edges can be directed or undirected
• Connected graph: there is a path between any two vertices

How many edges can there be in a connected graph with n vertices?

7 / 21
Graph: collection of vertices and edges
Graph G = (V , E): V = set of n vertices, E = set of m edges
• Edges can be directed or undirected
• Connected graph: there is a path between any two vertices

How many edges can there be in a connected graph with n vertices?

Sparse vs. dense graphs:


• Between n − 1 vs n(n − 1)/2 edges
7 / 21
Graph: collection of vertices and edges

Graph G = (V , E): V = set of n vertices (nodes), E = set of m edges

8 / 21
Graph: collection of vertices and edges

Graph G = (V , E): V = set of n vertices (nodes), E = set of m edges

How to represent a graph?


• Adjacency matrix: size n × n
• Avw = 1 if there is an edge between vertices v , w
(unweighted)
• n2 space required

8 / 21
Graph: collection of vertices and edges

Graph G = (V , E): V = set of n vertices (nodes), E = set of m edges

How to represent a graph?


• Adjacency matrix: size n × n
• Avw = 1 if there is an edge between vertices v , w
(unweighted)
• n2 space required

 
LON OSL ARN HEL AMS RIX
 LON 0 1 1 1 1 1 
 
 
 OSL 1 0 1 1 0 0 
 
ARN 1 1 0 1 0 0 
 
 HEL 1 1 1 0 1 1 
 
AMS 1 0 0 1 0 0 
 

RIX 1 0 0 1 0 0
8 / 21
Graph: adjacency lists
How to represent a graph?
• Adjacency list: connectivity represented using lists of edges
and vertices
• List of vertices (each vertex points to edges starting from it);
list of edges (each edge points to its endpoints)
• Linear space requirement in n (vertices), m (edges)
• In practice: a dictionary in Python

9 / 21
Graph: adjacency lists
How to represent a graph?
• Adjacency list: connectivity represented using lists of edges
and vertices
• List of vertices (each vertex points to edges starting from it);
list of edges (each edge points to its endpoints)
• Linear space requirement in n (vertices), m (edges)
• In practice: a dictionary in Python
• LON: {OSL, ARN, HEL, AMS, RIX}
• OSL: {LON, ARN, HEL}
• ARN: {LON, OSL, HEL}
• HEL: {LON, OSL, ARN, AMS, RIX}
• AMS: {LON, HEL}
• RIX: {LON, HEL}

9 / 21
Graph: adjacency lists
How to represent a graph?
• Adjacency list: connectivity represented using lists of edges
and vertices
• List of vertices (each vertex points to edges starting from it);
list of edges (each edge points to its endpoints)
• Linear space requirement in n (vertices), m (edges)
• In practice: a dictionary in Python
• LON: {OSL, ARN, HEL, AMS, RIX}
• OSL: {LON, ARN, HEL}
• ARN: {LON, OSL, HEL}
• HEL: {LON, OSL, ARN, AMS, RIX}
• AMS: {LON, HEL}
• RIX: {LON, HEL}
Adjacency matrices vs lists:
• Dense vs sparse graphs
• We’ll be using adjacency lists
9 / 21
Graph data structure
Class Digraph – (directed graph) methods:
• __init__: creates a new (empty) Digraph
• add_node(node): adds a node
• add_edge(edge): adds an edge
• children_of(node): returns all edges from node
• has_node(node): returns True if it is in the graph, False otherwise
• __str__: returns a string with all the edges of the graph

Class Graph(Digraph) – undirected graph:


• add_edge(edge): adds an edge and its reverse edge to the graph
• Example of inheritance: Graph does everything that Digraph
does, except that add_edge method is different

10 / 21
Today

1. Representing networks as graphs

2. Graph search

11 / 21
Searching a graph

12 / 21
Searching a graph

Small worlds phenomenon:


• Six degrees of separation (Marconi, Milgram, Guare)
• Hollywood: Kevin Bacon numbers

12 / 21
Example: social network
How to find the fewest steps to someone on a social network?

Idea:
• Me?
• My friends?
• Friends’ friends?
• Friends’ friends’ friends?
• ...

13 / 21
Example: social network
How to find the fewest steps to someone on a social network?

Idea:
• Me?
• My friends?
• Friends’ friends?
• Friends’ friends’ friends?
• ...

John Helena Paul Donald

Jared

Chris

Vicki

13 / 21
Example: social network
How to find the fewest steps to someone on a social network?

Idea:
• Me?
• My friends?
• Friends’ friends?
• Friends’ friends’ friends?
• ...

John Helena Paul Donald

Jared

Chris

Vicki

13 / 21
Example: social network
How to find the fewest steps to someone on a social network?

Idea:
• Me?
• My friends?
• Friends’ friends?
• Friends’ friends’ friends?
• ...

John Helena Paul Donald

Jared

Chris

Vicki

13 / 21
Example: social network
How to find the fewest steps to someone on a social network?

Idea:
• Me?
• My friends?
• Friends’ friends?
• Friends’ friends’ friends?
• ...

John Helena Paul Donald

Jared

Chris

Vicki

13 / 21
Example: social network
How to find the fewest steps to someone on a social network?

Idea:
• Me?
• My friends?
• Friends’ friends?
• Friends’ friends’ friends?
• ...

John Helena Paul Donald

Jared

Chris

Vicki

13 / 21
Breadth-first search (BFS)

• Explore nodes in layers

Plan:
1. How to explore a graph in layers?
2. How much work is it to go through the entire
graph?
3. How to get shortest distances and paths?

14 / 21
Exploring in layers: BFS algorithm
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q

15 / 21
Exploring in layers: BFS algorithm
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q

What is a queue?
• First in, first out (just like in a cafe)
• Remove from front, insert to back in constant time O(1)
• Here we keep a queue of nodes to visit next

15 / 21
Exploring in layers: BFS algorithm
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:

What is a queue?
• First in, first out (just like in a cafe)
• Remove from front, insert to back in constant time O(1)
• Here we keep a queue of nodes to visit next

15 / 21
Exploring in layers: BFS algorithm
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v

What is a queue?
• First in, first out (just like in a cafe)
• Remove from front, insert to back in constant time O(1)
• Here we keep a queue of nodes to visit next

15 / 21
Exploring in layers: BFS algorithm
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

What is a queue?
• First in, first out (just like in a cafe)
• Remove from front, insert to back in constant time O(1)
• Here we keep a queue of nodes to visit next

15 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices: John


16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices: Helena


16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices: Helena Chris


16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices: Chris


16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices: Chris


16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices: Chris Paul


16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices: Paul


16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices: Paul


16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices: Paul


16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices: Paul Vicki


16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices: Vicki


16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices: Vicki


16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices: Vicki Jared


16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices: Jared


16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices: Jared


16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices:
16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices:
16 / 21
BFS example
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

John Helena Paul Donald

Jared
Chris

Vicki

Queue of vertices: Donald


16 / 21
How much work is it?
Complete exploration: At the end, any given node v is explored if
and only if there is a path from s to v

17 / 21
How much work is it?
Complete exploration: At the end, any given node v is explored if
and only if there is a path from s to v

Running time?
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

17 / 21
How much work is it?
Complete exploration: At the end, any given node v is explored if
and only if there is a path from s to v

Running time?
Algorithm: BFS(graph G, start node s)
• Initialise: mark all nodes unexplored except s explored
• Use Q = queue data structure; add s to Q
• Main loop: While Q is not empty:
• Remove the first node of Q and call it v
• For each edge (v , w): if w unexplored:
• Mark w explored
• Add w to Q

Work for each of n nodes Work for each of m edges


• •
• •
• •
17 / 21
How to get shortest distances?
• Fewest edges to cross to get from s to v
• This is the same as how many “layers of friends” we have to cross, or
degrees of separation

18 / 21
How to get shortest distances?
• Fewest edges to cross to get from s to v
• This is the same as how many “layers of friends” we have to cross, or
degrees of separation

To get shortest distance, we need to add:

Initialise: dist(v ) = 0 for v = s, ∞ for other v

Main loop: when going through edge (v , w): if w unexplored, set


dist(w) = dist(v ) + 1

18 / 21
How to get shortest distances?
• Fewest edges to cross to get from s to v
• This is the same as how many “layers of friends” we have to cross, or
degrees of separation

To get shortest distance, we need to add:

Initialise: dist(v ) = 0 for v = s, ∞ for other v

Main loop: when going through edge (v , w): if w unexplored, set


dist(w) = dist(v ) + 1

John Helena Paul Donald

Jared

Chris

Vicki

18 / 21
We can also check if the graph is connected

Connected components = the “pieces” of (undirected) graph G


• Check if graph is connected
• Visualise graphs
• Clustering

How to do it:
• If BFS finishes without exploring all nodes, restart it from an
unexplored node
• O(m + n) time (the size of the entire graph)

19 / 21
Graphs are useful

Many problems revolve around transitions from one system state


to another
• Graph search often useful
• Examples: travelling, scheduling, etc.

For unweighted graphs, many problems solved by BFS or DFS -


depth-first search
• Linear time
• DFS explores like a maze — optional problem
• Weighted graphs require different methods

20 / 21
Review

Many problems can be modelled using graphs


• Graph representation: adjacency list or adjacency
matrix
• Graphs can be (un)directed, (un)weighted
• Breadth-first search for shortest paths in unweighted
graphs

Review exercises:
• BFS implementation
• Optional: apply BFS to a Hollywood dataset
• Optional: Dijkstra’s algorithm for weighted graphs

21 / 21
Exercises

Session zip file ses07.zip on the Hub


• HTML instructions
• At some point, you’ll need the ses07.py-file with
skeleton code (open in Spyder)

You might also like