39
GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

Embed Size (px)

Citation preview

Page 1: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

1

GRAPHS

CS16: Introduction to Data Structures & Algorithms

Thursday, March 5, 2015

Page 2: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

2

Outline

1) What is a graph?

2) Terminology

3) Properties

4) Graph Types

5) Representations

6) Performance

7) BFS/DFS

8) Applications

Thursday, March 5, 2015

Page 3: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

3

What is a graph?

• A graph defined by:• a set of vertices (V)• a set of edges (E)

• Vertices and edges can both store data

Thursday, March 5, 2015

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233337

2555

142

• Example:• A vertex represents an airport and stores the 3-letter

airport code• An edge represents a flight route between 2 airports and

stores the mileage of the route

Page 4: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

4

Terminology

Thursday, March 5, 2015

• End vertices (or endpoints) of an edge• U and V are the endpoints of a

• Incident edges on a vertex• a, d, and b are incident on V

• Adjacent vertices• U and V are adjacent

• Degree of a vertex• X has degree 5

• Parallel (multiple) edges• h and i are parallel edges

• Self-loop• j is a self-loop

XU

V

W

Z

Y

a

c

b

e

d

f

g

h

i

j

Page 5: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

5

Terminology (2)• Path

• sequence of alternating vertices and edges

• begins with a vertex• ends with a vertex• each edge is preceded and

followed by its endpoints• Simple path

• path such that all its vertices and edges are visited at most once

• Examples• P1=(V–b->X–h->Z) is a simple path• P2=(U–c->W–e->X–g->Y–f->W–d->V) is not a simple

path, but is still a path

Thursday, March 5, 2015

P1

XU

V

W

Z

Y

a

c

b

e

d

f

g

hP2

Page 6: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

6

Graph Properties

• A graph G’ = (V’, E’) is a subgraph of G = (V, E) if V’ is contained in V and E’ is contained in E

• A graph is connected if there exists a path from each vertex to every other vertex in G.

• A cycle is a path that starts and ends at the same vertex

• A graph is acyclic if no subgraph is a cycle

Thursday, March 5, 2015

Page 7: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

7

Graph Properties (2)

• A spanning tree is a subgraph that contains all the graph’s vertices in a single tree and enough edges to connect each vertex without cycles.

Thursday, March 5, 2015

ORDPVD

MIADFW

LGA

ORDPVD

MIADFW

LGA

Graph G Spanning Tree for G

Page 8: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

8

Graph Properties (3)

• A spanning forest is a subgraph that consists of a spanning tree in each connected component of a graph

• Spanning forests never contain cycles. Keep in mind that this might not be the “best” or shortest path to each node.

Thursday, March 5, 2015

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

Page 9: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

9

Graph Properties (4)

• A graph G is a tree if and only if it satisfies any of the following 5 conditions:• G has V-1 edges and no cycles• G has V-1 edges and is connected• G is connected, but removing any edge disconnects it

• G is acyclic, but adding any edges creates a cycle

• Exactly one simple path connects each pair of vertices in G

Thursday, March 5, 2015

Page 10: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

10

Graph Proof 1

• Prove that the sum of the degrees of all the vertices of some graph G equals twice the number of edges of G.

• Let V = {v1, v2, …, vp}, where p is the number of

vertices in G. When computing the total sum of degrees D, we see that

D = deg(v1) + deg (v2) + … + deg(vp).

• Notice that each edge is counted twice in D: one for each of the two vertices incident on an edge. Thus D = 2*|E|, where |E| is the number of edges.

Thursday, March 5, 2015

Page 11: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

11

Graph Proof 2• Using induction prove that in every connected graph:

|E| ≥ |V| – 1• Base Case: A graph with one vertex will have 0 edges, and 0 ≥ 1 – 1• Assume: In a graph with k vertices, |E| ≥ k – 1 • Let G be any connected graph with k + 1 vertices. We must show

that |E| ≥ k. Let u be the vertex of minimum degree in G. • If deg(u) = 1:

• u must be a leaf. Let G’ be G without u and its 1 incident edge. G’ is clearly still connected, because we only removed a leaf. By our assumption, G’ has at least k – 1 edges, which means that G has at least k edges.

• If deg(u) ≥ 2:• Every vertex has at least two incident edges, so the total degree

(D) of the graph is: D ≥ 2(k+1)• Since we know from the last proof that D = 2*|E|:

D ≥ 2(k+1) 2|E| ≥ 2(k+1) |E| ≥ k + 1 |E| ≥ k• True for 0, and for k+1 assuming k, so true for all n≥0.

Thursday, March 5, 2015

Page 12: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

12

Graph Types

• Directed graph• all the edges are directed• ordered pair of vertices (u,v)• first vertex u is the origin• second vertex v is the

destination• e.g., a flight

• Undirected graph• all the edges are undirected• unordered pair of vertices

(u,v)• e.g., a flight route

Thursday, March 5, 2015

ORD PVD

flightAA 600

ORD PVD

route849 miles

Page 13: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

Graph Types: Directed Graph

Thursday, March 5, 2015 13

ORD

DFW

SFO

LAX

US

32

SW 223

AA 123

PA 23

LH 21

Page 14: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

CS32

CS16

CS18

CS15

CS17

CS19

CS123 CS224

CS141 CS242

CS125CS128

CS22

Acyclic = without cycles

means ‘is a prerequisite for’

Thursday, March 5, 2015 14

We’ll talk much more about DAGs in future lectures…

Directed Acyclic Graph (DAG)

Page 15: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

15

Undirected Graph

Thursday, March 5, 2015

ORDPVDF

MIADFW

SFO

LAX

LGA

HNL

849

802

13871743

1843

10991120

1233

337

2555

142

Page 16: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

16

Graph Representations

• Vertices usually stored in a list or hash set• Three common methods of representing which vertices are adjacent:• Edge list (or set)• Adjacency lists (or sets)• Adjacency matrix

Thursday, March 5, 2015

Page 17: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

17

Edge List (or Set)• Way of representing which

vertices are adjacent to each other as a list of pairs

• Each element in the list is a single edge (a,b) from node a to node b

• Since order of this list doesn’t matter, we can use a single hash set instead to improve runtime

Thursday, March 5, 2015

Edge List:

[ (1,1), (1,2), (1,5), (2,3), (2,5), (3,4), (4,5), (4,6) ]

Page 18: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

18

Adjacency Lists (or Sets)• Another way of representing

which vertices are adjacent to each other

• Each vertex has an associated list representing the vertices it neighbors

• Since order of these lists doesn’t matter, they could be hash sets instead to make lookup faster

Thursday, March 5, 2015

1 2 3 4 5 6

[1,2,5] [1,3,5] [2,4] [3,5,6] [1,2,4] [4]

Page 19: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

19

Sets

Thursday, March 5, 2015

• Remember finding an element in a hashset is a constant operation:

A = { (1,1), (1,2), (1,5), (2,3), (2,5), (3,4), (4,5), (4,6) }

A.contains((4,5)) is O(1)

• Adjacency List elements can also be hashsets:

B = [ {1, 2, 5}, {1, 3, 5}, {2, 4}, {3, 5, 6}, {1, 2, 4}, {4} ]

B[2].contains(5) is still O(1)

• They can also be implemented as hashsets of hashsets! Oh boy!

Page 20: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

20

Adjacency Matrices

• Another way of representing which vertices are adjacent to each other

• Matrix is n x n, where n is the number of nodes in the graph• true = edge• false = no edge

• If m[u][v] is true, then node u has an edge to node v (and if the graph is undirected, we can assume the opposite is true as well)

Thursday, March 5, 2015

Page 21: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

21

Adjacency Matrices (2)

Thursday, March 5, 2015

1 2 3 4 5 6

1 T T F F T F

2 T F T F T F

3 F T F T F F

4 F F T F T T

5 T T F T F F

6 F F F T F F

Page 22: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

22

Adjacency Matrices (3)

• We initialize the matrix to the predicted size of our graph (we can always expand the array later)

• When a vertex is added to the graph, we reserve a row and column of the matrix for that vertex

• When a vertex is removed, its row and column are set to false• Since we can’t remove rows/columns from arrays,

we keep a separate list of vertices to represent vertices actually present in the graph

Thursday, March 5, 2015

Page 23: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

Main Methods of the Graph ADT

• Vertices and edges• store values

• Ex: edge weights

• Accessor methods• vertices()• edges()• incidentEdges(vertex)• areAdjacent(v1, v2)• endVertices(edge)• opposite(vertex, edge)

• Update methods• insertVertex(value)• insertEdge(v1, v2)

• Note: Sometimes this function also takes a value!insertEdge(v1, v2, val)

• removeVertex(vertex)• removeEdge(edge)

Thursday, March 5, 2015 23

Page 24: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

24

Thursday, March 5, 2015

Big-O Performance:

Edge Set Adjacency Sets Adjacency Matrix

Overall Space |V| + |E| |V| + |E| |V|2

vertices()1 1* 1* 1*

edges() 1* |E| |V|2

incidentEdges(v) |E| 1* |V|

areAdjacent (v1, v2) 1 1 1

insertVertex(val) 1 1 |V|

insertEdge(v1, v2) 1 1 1

removeVertex(v) |E| |V| |V|

removeEdge(v1, v2) 1 1 1

Thursday, March 5, 2015

24

1 In all three approaches, we maintain an additional list or set of vertices.* in-place

Page 25: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

25

Big-O Performance (Edge Set)

Thursday, March 5, 2015

Operation Runtime Explanation

vertices() O(1) Return the set of vertices

edges() O(1) Return the set of edges

incidentEdges(v) O(|E|) Iterate through each edge and check if it contains vertex v

areAdjacent(v1,v2) O(1) Check if edge (v1,v2) exists in the set

insertVertex(v) O(1) Add vertex v to the vertex list

insertEdge(v1,v2) O(1) Add element (v1,v2) to the set

removeVertex(v) O(|E|) Iterate through each edge and remove it if it has vertex v

removeEdge(v1,v2) O(1) Remove edge (v1,v2)

Page 26: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

26

Big-O Performance (Adjacency Set)

Thursday, March 5, 2015

Operation Runtime Explanation

vertices() O(1) Return the set of vertices

edges() O(|E|) Concatenate each vertex with its subsequent vertices

incidentEdges(v) O(1) Return v’s edge set

areAdjacent(v1,v2) O(1) Check if v2 is in v1’s set

insertVertex(v) O(1) Add vertex v to the vertex set

insertEdge(v1,v2) O(1) Add v1 to v2’s edge set and vice versa

removeVertex(v) O(|V|) Remove v from each of its adjacent vertices’ sets and remove v’s set

removeEdge(v1,v2) O(1) Remove v1 from v2’s set and vice versa

Page 27: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

27

Big-O Performance (Adjacency Matrix)

Thursday, March 5, 2015

Operation Runtime Explanation

vertices() O(1) Return the set of vertices

edges() O(|V|2) Iterate through the entire matrix

incidentEdges(v) O(|V|) Iterate through v’s row or column to check for truesNote: row/col are the same in an undirected graph.

areAdjacent(v1,v2) O(1) Check index (v1,v2) for a true

insertVertex(v) O(|V|)* Add vertex v to the matrix (*amortized)

insertEdge(v1,v2) O(1) Set index (v1,v2) to true

removeVertex(v) O(|V|) Set v’s row and column to false and remove v from the vertex list

removeEdge(v1,v2) O(1) Remove v1 from v2’s set and vice versa

Page 28: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

28

BFT/DFT

• Do you remember when we performed breadth first traversals and depth first traversals on trees?

• These traversals can also be applied to graphs! (A tree is just a special kind of graph, after all…)• Can be used to traverse and “visit” nodes in a graph (BFT / DFT)

• Often used to search for a certain value in a graph (BFS / DFS)

Thursday, March 5, 2015

Page 29: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

29

Thursday, March 5, 2015

Breadth First Traversal: Tree vs. Graph

Thursday, March 5, 2015

function treeBFT(root): //Input: Root node of tree //Output: Nothing Q = new Queue() Q.enqueue(root) while Q is not empty: node = Q.dequeue() doSomething(node) enqueue node’s children

function graphBFT(start): //Input: start vertex //Output: Nothing Q = new Queue() start.visited = true Q.enqueue(start) while Q is not empty: node = Q.dequeue() doSomething(node) for neighbor in node’s adjacent nodes: if not neighbor.visited: neighbor.visited = true Q.enqueue(neighbor)

29

doSomething(node) could print, add to list, decorate nodes, etc.

Mark nodes as visited, otherwise you might loop forever!

Page 30: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

30

Depth First Traversal

• One way to perform a DFT a graph is to use a stack instead of a queue as in BFT

• DFT can also be done recursively

Thursday, March 5, 2015

function recursiveDFT(node): // Input: start node // Output: Nothing node.visited = true for neighbor in node’s adjacent vertices: if not neighbor.visited: recursiveDFT(neighbor)

Page 31: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

31

Applications• Flight networks• GPS maps• The internet

• pages are nodes• links are edges

• Facebook Graph• Modeling molecular

structures• atoms are nodes• bonds are edges

• So many more!!

Thursday, March 5, 2015

John

DavidPaul

brown.edu

cox.net

cs.brown.edu

att.netqwest.net

math.brown.edu

cslab1bcslab1a

Page 32: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

32

Applications: Flight Paths Exist• Problem: Given an undirected graph with airport (vertices) and flights

(edges), determine whether it is possible to fly from one airport to another.

• Strategy: Use a breadth first search starting at the first node, and determine if the ending airport is ever visited.

Thursday, March 5, 2015

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

PWM JFK

Page 33: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

33

Applications: Flight Paths Exist (2)

Thursday, March 5, 2015

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

PWM JFK

• Task: Is there a path from SFO to PVD?

Page 34: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

34

Applications: Flight Paths Exist (2)

Thursday, March 5, 2015

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

PWM JFK

• Task: Is there a path from SFO to PVD?

Page 35: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

35

Applications: Flight Paths Exist (2)

Thursday, March 5, 2015

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

PWM JFK

• Task: Is there a path from SFO to PVD?

Page 36: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

36

Applications: Flight Paths Exist (2)

Thursday, March 5, 2015

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

PWM JFK

• Task: Is there a path from SFO to PVD?

YES, now how do we do it in code?

Page 37: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

37

Flight Paths Exist Pseudocode

Thursday, March 5, 2015

function pathExists(from, to): //Input: from: vertex, to: vertex //Output: true if path exists, false otherwise Q = new Queue() from.visited = true Q.enqueue(from) while Q is not empty: airport = Q.dequeue() if airport == to: return true for neighbor in airport’s adjacent nodes: if not neighbor.visited: neighbor.visited = true Q.enqueue(neighbor) return false

Page 38: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

38

Applications: Flight Layovers• Problem: Given an undirected graph with airport (vertices) and flights

(edges), decorate the vertices with the least number of stops from a given source. If there is no way to get to a certain airport, decorate node with infinity.

• Strategy: Decorate each node with an initial ‘stop value’ of infinity. Use breadth first search to decorate each node with a ‘stop value’ of one greater than its previous.

Thursday, March 5, 2015

ORDPVD

MIADFW

SFO

LAX

LGA

HNL

PWM JFK

Page 39: GRAPHS CS16: Introduction to Data Structures & Algorithms Thursday, March 5, 2015 1

39

Flight Layover Pseudocodefunction numStops(G, source): //Input: G: graph, source: vertex //Output: Nothing //Purpose: decorate each vertex with the lowest number of // layovers from source. for every node in G: node.stops = infinity

Q = new Queue() source.stops = 0 source.visited = true Q.enqueue(source) while Q is not empty: airport = Q.dequeue() for neighbor in airport’s adjacent nodes: if not neighbor.visited: neighbor.visited = true neighbor.stops = airport.stops + 1 Q.enqueue(neighbor)

Thursday, March 5, 2015