Graphs CS16: Introduction to Data Structures & Algorithms - - PowerPoint PPT Presentation

graphs
SMART_READER_LITE
LIVE PREVIEW

Graphs CS16: Introduction to Data Structures & Algorithms - - PowerPoint PPT Presentation

Graphs CS16: Introduction to Data Structures & Algorithms Spring 2020 Outline What is a Graph Terminology Properties Graph Types Representations Performance BFS/DFS Applications 2 What is a Graph A graph


slide-1
SLIDE 1

Graphs

CS16: Introduction to Data Structures & Algorithms Spring 2020

slide-2
SLIDE 2

Outline

  • What is a Graph
  • Terminology
  • Properties
  • Graph Types
  • Representations
  • Performance
  • BFS/DFS
  • Applications
2
slide-3
SLIDE 3

What is a Graph

  • A graph is defined by
  • a set of vertices (or vertexes, or nodes) V
  • a set of edges E
  • Vertices and edges can both store data
slide-4
SLIDE 4

Example: Social Graph

Kieran Healy, “Using metadata to find Paul Revere”

https://kieranhealy.org/blog/archives/2013/06/09/using-metadata-to-find-paul-revere/

2 1 2 3 2 2 4 3 2 1

slide-5
SLIDE 5

Terminology

  • Endpoints or end vertices of an edge
  • U and V are endpoints of edge a
  • Incident edges of a vertex
  • a, b, d are incident to V
  • Adjacent vertices
  • U and V are adjacent
  • Degree of a vertex
  • X has degree of 5
  • Parallel (multiple) edges
  • h, i are parallel edges
  • Self-loops
  • j is a self-looped edge
5 X U V W Z Y a c b e d f g h i j
slide-6
SLIDE 6

Terminology

  • A path is a sequence of alternating

vertices and edges

  • begins and 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 6 P1 X U V W Z Y a c b e d f g h P2
slide-7
SLIDE 7

Applications

  • Flight networks
  • Road networks & GPS
  • The Web
  • pages are vertices
  • links are edges
  • The Internet
  • routers and devices are vertices
  • network connections are edges
  • Facebook
  • profiles are vertices
  • friendships are edges
7
slide-8
SLIDE 8

Graph Properties

  • A graph G’=(V’,E’) is a subgraph of G=(V,E)
  • if V’ ⊆ V and E’ ⊆ E
  • A graph is connected if
  • there exists path from each vertex

to every other vertex

  • A path is a cycle if
  • it starts and ends at the same vertex
  • A graph is acyclic
  • if it has no cycles
8
slide-9
SLIDE 9

A Subgraph

2 1 2 3 2 2 4 3 2 1

slide-10
SLIDE 10

Connected?

2 1 2 3 2 2 4 3 2 1

slide-11
SLIDE 11

Connected?

1 2 3 2 4 1

2 connected components
slide-12
SLIDE 12

Cycles

2 1 2 3 2 2 4 3 2 1

slide-13
SLIDE 13

Acyclic?

2 1 2 3 2 2 4 3 2 1

slide-14
SLIDE 14

Graph Properties

  • A spanning tree of G is a subgraph with
  • all of G’ s vertices in a single tree
  • and enough edges to connect each vertex w/o cycles
14
slide-15
SLIDE 15

Spanning tree

2 1 2 2 4 3 2 3 2 1

slide-16
SLIDE 16

Graph Properties

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

connected component of graph

  • Spanning forests never contain cycles
  • this might not be the “best” or shortest path to each

node

16 ORD PVD MIA DFW SFO LAX LGA HNL
slide-17
SLIDE 17

Spanning forest

1 2 4 2

slide-18
SLIDE 18

Graph Properties

  • G is a tree if and only if it satisfies any of these

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

18
slide-19
SLIDE 19

Graph Proof 1

  • Prove that
  • the sum of the degrees of all vertices of some graph G…
  • …is twice the number of edges of G
  • Let V = {v1,v2,…,vp}, where p is number of vertices
  • The total sum of degrees D is such that
  • D = deg(v1) + deg(v2) + … + deg(vp)
  • But each edge is counted twice in D
  • one for each of the two vertices incident to the edge
  • So D = 2|E|, where |E| is the number of edges.
19
slide-20
SLIDE 20

Graph Proof 2

  • Prove using induction that if G is connected then
  • |E| ≥ |V|–1, for all |V|≥1
  • Base case |V|=1
  • If graph has one vertex then it will have 0 edges
  • so since |E|=0 and |V|-1=1-1=0, we have |E| ≥|V|-1
  • Inductive hypothesis
  • If graph has |V|=k vertices then |E|≥k–1
  • Inductive step
  • Let G be any connected graph with |V|=k+1 vertices
  • We must show that |E|≥k
20
slide-21
SLIDE 21

Graph Proof 2

  • Inductive step
  • Let G be any connected graph with |V|=k+1 vertices
  • We must show that |E| ≥ k
  • Let u be the vertex of minimum degree in G
  • deg(u) ≥ 1 since G is connected
  • If deg(u) = 1
  • Let G’ be G without u and its 1 incident edge
  • G’ has k vertices because we removed 1 vertex from G
  • G’ is still connected because we only removed a leaf
  • So by inductive hypothesis, G’ has at least k–1 edges
  • which means that G has at least k edges
21
slide-22
SLIDE 22

Graph Proof 2

  • 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)
  • But we know from the last proof that D=2|E|
  • so 2|E| ≥ 2(k+1) ⟹ |E| ≥ k+1 ⟹ |E|≥k
  • We showed it is true for |V|=1 (base case)…
  • …and for |V|=k+1 assuming it is true for |V|=k…
  • …so it is true for all |V|≥1
22
slide-23
SLIDE 23

Undirected graph

2 1 2 3 2 2 4 3 2 1

slide-24
SLIDE 24

Directed graph

The British are coming! Cycle? Cycle?
slide-25
SLIDE 25

Edge Types

  • Undirected edge
  • unordered pair of vertices (L,R)
  • Directed edge
  • ordered pair of vertices (L,R)
  • first vertex L is the origin
  • second vertex R is the destination
25
slide-26
SLIDE 26

Directed Acyclic Graph (DAG)

26 CS32 CS16 CS18 CS15 CS17 CS19 CS123 CS224 CS141 CS242 CS125 CS128 CS22 Acyclic = without cycles means ‘is a prerequisite for’ We’ll talk much more about DAGs in future lectures…
slide-27
SLIDE 27

Graph Representations

  • Vertices usually stored in a List or Set
  • 3 common ways of representing which vertices

are adjacent

  • Edge list (or set)
  • Adjacency lists (or sets)
  • Adjacency matrix
27
slide-28
SLIDE 28

Edge List

  • Represents adjacencies as a list of pairs
  • Each element of list is a single edge (a,b)
  • Since the order of list doesn’t matter
  • can use hashset to improve runtime of adjacency testing
28 [(1,1),(1,2),(1,5),(2,3),(2,5),(3,4),(4,5),(4,6)]
slide-29
SLIDE 29

Edge Set

  • Store all the edges in a Hashset
29 (1,1) (3,4) (1,5) (4,5) (2,5) (4,6) (1,2) (2,3)
slide-30
SLIDE 30

Adjacency Lists

  • Each vertex has an associated list with its neighbors
  • Since the order of elements in lists doesn’t matter
  • lists can be hashsets instead
30 1 2 3 4 5 6

1 2 5 1 3 5 2 4 3 5 6 1 2 4 4

slide-31
SLIDE 31

Adjacency Set

  • Each vertex associated Hashset of its neighbors
31 1 2 3 4 5 6

Hashset of {1,2,5} Hashset of {1,3,5} Hashset of {2,4} Hashset of {3,5,6} Hashset of {1,2,4} Hashset of {4}

slide-32
SLIDE 32

Adjacency Matrix

  • Matrix with n rows and n columns
  • n is number of vertices
  • If u is adjacent to v then M[u,v]=T
  • If u is not adjacent to v then M[u,v]=F
  • If graph is undirected then M[u,v]=M[v,u]
32
slide-33
SLIDE 33

Adjacency Matrix

33 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
slide-34
SLIDE 34

Adjacency Matrix

  • Initialize matrix to predicted size of graph
  • we can always expand later
  • When vertex is added to graph
  • reserve a row and column of matrix for that vertex
  • When vertex is removed
  • set its entire row and column to false
  • Since we can’t remove rows/columns from arrays
  • keep separate collection of vertices that are actually present

in graph

34
slide-35
SLIDE 35

Graph ADT

  • Vertices and edges can 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)
  • sometimes this function also
takes a value so insertEdge(v1, v2,val)
  • removeVertex(vertex)
  • removeEdge(edge)
slide-36
SLIDE 36

Big-O Performance

36

3 min

Activity #1

slide-37
SLIDE 37

Big-O Performance

37

3 min

Activity #1

slide-38
SLIDE 38

Big-O Performance

38

2 min

Activity #1

slide-39
SLIDE 39

Big-O Performance

39

1 min

Activity #1

slide-40
SLIDE 40

Big-O Performance

40

0 min

Activity #1

slide-41
SLIDE 41

Big-O Performance

41 Edge Set Adjacency Sets Adjacency Matrix Overall Space1 O(|V| + |E|) O(|V| + |E|) O(|V|2) vertices( )1 O(1)* O(1)* O(1)* edges( ) O(1)* O(|E|) O(|V|2) incidentEdges(v) O(|E|) O(1)* O(|V|) areAdjacent (v1, v2) O(1) O(1) O(1) insertVertex(v) O(1) O(1) O(|V|) insertEdge(v1, v2) O(1) O(1) O(1) removeVertex(v) O(|E|) O(|V|) O(|V|) removeEdge(v1, v2) O(1) O(1) O(1) * in place (return pointer) 1 In all approaches, we maintain an additional list or set of vertices
slide-42
SLIDE 42

Big-O Performance (Edge Set)

42

Operation Runtime Explanation

vertices() O(1)

Return set of vertices

edges() O(1)

Return 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 (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)
slide-43
SLIDE 43

Big-O Performance (Adjacency Set)

43

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
slide-44
SLIDE 44

Big-O Performance (Adjacency Matrix)

44

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 trues Note: 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 (* O(1) 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)

Set index (v1,v2) to false
slide-45
SLIDE 45

BFT and DFT

  • Remember BFT and DFT on trees?
  • We can also do them on graphs
  • a tree is just a special kind of graph
  • often used to find certain values in graphs
45
slide-46
SLIDE 46

BFT/DFT on Graphs

46

1 min

Activity #2

slide-47
SLIDE 47

BFT/DFT on Graphs

47

1 min

Activity #2

slide-48
SLIDE 48

BFT/DFT on Graphs

48

0 min

Activity #2

slide-49
SLIDE 49

Breadth First Traversal: Tree vs. Graph

49 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 adj nodes: if not neighbor.visited: neighbor.visited = true Q.enqueue(neighbor)

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

Mark nodes as visited otherwise you will loop forever!
slide-50
SLIDE 50

Depth First Traversal

  • To do DFT on graph, replace queue with stack
  • Can also be done recursively
50 function recursiveDFT(node): // Input: start node // Output: Nothing node.visited = true for neighbor in node’s adjacent vertices: if not neighbor.visited: recursiveDFT(neighbor)
slide-51
SLIDE 51

Applications: Flight Paths Exist

  • Given undirected graph with airports & flights
  • is it possible to fly from one airport to another?
  • Strategy
  • use breadth first search starting at first node
  • and determine if ending airport is ever visited
51 ORD PVD MIA DFW SFO LAX LGA HNL BTV JFK
slide-52
SLIDE 52

Applications: Flight Paths Exist

  • Is there flight from SFO to PVD?
52 ORD PVD MIA DFW SFO LAX LGA HNL BTV JFK
slide-53
SLIDE 53

Applications: Flight Paths Exist

  • Is there flight from SFO to PVD?
53 ORD PVD MIA DFW SFO LAX LGA HNL BTV JFK
slide-54
SLIDE 54

Applications: Flight Paths Exist

  • Is there flight from SFO to PVD?
54 ORD PVD MIA DFW SFO LAX LGA HNL BTV JFK
slide-55
SLIDE 55

Applications: Flight Paths Exist

  • Is there flight from SFO to PVD?
  • Yes! but how do we do it with code?
55 ORD PVD MIA DFW SFO LAX LGA HNL BTV JFK
slide-56
SLIDE 56

Flight Paths Exist Pseudo-Code

56 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
slide-57
SLIDE 57

Applications: Flight Layovers

  • Given undirected graph with airports & flights
  • decorate vertices w/ least number of

stops from a given source

  • if no way to get to a an airport decorate w/ ∞
  • Strategy
  • decorate each node w/ initial ‘stop value’ of ∞
  • use breadth first search to decorate each node…
  • …w/ ‘stop value’ of one greater than its previous value
57
slide-58
SLIDE 58

Flight Layovers Pseudo-Code

58 function 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)
slide-59
SLIDE 59

Flight Layovers Pseudo-Code

59 ORD PVD MIA DFW SFO LAX LGA HNL BTV JFK

∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞

slide-60
SLIDE 60

Flight Layovers Pseudo-Code

60 ORD PVD MIA DFW SFO LAX LGA HNL BTV JFK

∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ✓

HNL

1 ✓

LAX

2 ✓

SFO

2 ✓

DFW

2 ✓

ORD

3 ✓

PVD

3 ✓

LGA

3 ✓

MIA