Graphs Slide credits: K. Wayne, Princeton U. C. E. Leiserson and E. Demaine, MIT K. Birman,...

25
Graphs Slide credits: K. Wayne, Princeton U. C. E. Leiserson and E. Demaine, MIT K. Birman, Cornell U.

description

These are Graphs K5K5 K 3,3 =

Transcript of Graphs Slide credits: K. Wayne, Princeton U. C. E. Leiserson and E. Demaine, MIT K. Birman,...

Page 1: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Graphs

Slide credits: K. Wayne, Princeton U. C. E. Leiserson and E. Demaine, MIT K. Birman, Cornell U.

Page 2: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

These are not Graphs

...not the kind we mean, anyway

Page 3: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

These are Graphs

K5 K3,3

=

Page 4: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Applications of Graphs

Communication networks Routing and shortest path problems Commodity distribution (flow) Traffic control Resource allocation Geometric modeling ...

Page 5: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Undirected Graphs

Undirected graph. G = (V, E) is an ordered pair consisting of V = nodes. E = edges between pairs of nodes. Captures pairwise relationship between objects. Graph size parameters: n = |V|, m = |E|. Two nodes connected by an edge are said to be neighbor.

V = { 1, 2, 3, 4, 5, 6, 7, 8 }E = { 1-2, 1-3, 2-3, 2-4, 2-5, 3-5, 3-7, 3-8, 4-5, 5-6 }n = 8m = 11

Page 6: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
Page 7: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Some Properties of Undirected Graph

|E|= O(V2) If G is connected |E|≥|V|–1

– An undirected graph is connected if for every pair of nodes u

and v, there is a path between u and v. deg(V) is defined as the number of edges incident to V

– Handshaking lemma: Σv∈V deg(v) = 2|E|

Page 8: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Directed Graphs

Directed graph. G = (V, E) Edge (u, v) goes from node u to node v.

Ex. Web graph - hyperlink points from one web page to another. Directedness of graph is crucial. Modern web search engines exploit hyperlink structure to rank

web pages by importance.

Page 9: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

World Wide Web

Web graph. Node: web page. Edge: hyperlink from one page to another.

cnn.com

cnnsi.comnovell.comnetscape.com timewarner.com

hbo.com

sorpranos.com

Page 10: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Graph Representation: Adjacency Matrix

Adjacency matrix. n-by-n matrix with Auv = 1 if (u, v) is an edge. Two representations of each edge. Space proportional to n2. Checking if (u, v) is an edge takes O(1) time. Identifying all edges takes O(n2) time. Use for dense graph

1 2 3 4 5 6 7 81 0 1 1 0 0 0 0 02 1 0 1 1 1 0 0 03 1 1 0 0 1 0 1 14 0 1 0 1 1 0 0 05 0 1 1 1 0 1 0 06 0 0 0 0 1 0 0 07 0 0 1 0 0 0 0 18 0 0 1 0 0 0 1 0

Page 11: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Graph Representation: Adjacency List

Adjacency list. Node indexed array of lists. Two representations of each edge. Space proportional to m + n. Checking if (u, v) is an edge takes O(deg(u)) time. Identifying all edges takes O(m + n) time. Use for sparse graph

1 2 3234 2 55

67 3 88

1 3 4 51 2 5 87

2 3 4 65

degree = number of neighbors of u

3 7

Page 12: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Paths and Connectivity

Def. A path in an undirected graph G = (V, E) 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.

Def. A path is simple if all nodes are distinct.

Def. An undirected graph is connected if for every pair of nodes u and v, there is a path between u and v.

Page 13: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Cycles

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

cycle C = 1-2-4-5-3-1

Page 14: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Trees

Def. An undirected graph is a tree if it is connected and does not contain a cycle.

Theorem. Let G be an undirected graph on n nodes. Any two of the following statements imply the third.

G is connected. G does not contain a cycle. G has n-1 edges.

Page 15: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Rooted Trees

Rooted tree. Given a tree T, choose a root node r and orient each edge away from r.

Importance. Models hierarchical structure.

a tree the same tree, rooted at 1

v

parent of v

child of v

root r

Page 16: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Graph Traversal

Problem: Search for a certain node or traverse all nodes in the graph

Depth First Search– Once a possible path is found, continue the search until

the end of the path Breadth First Search

– Start several paths at a time, and advance in each one step at a time

Page 17: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Depth First Traversal

A natural way to do Depth-first search (BFS) is using recursion.

DFS( Node c ) {Mark c "Visited”For each neighbor n of c

If n "Unvisited" DFS( n )}

Possible visit sequence: 1, 2, 4, 5, 6, 3, 8, 7

Page 18: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Breadth First Traversal

Breadth-first search (BFS) not naturally recursive. Use a queue so that vertices are visited in order according

to their distance from the starting vertex.

BFS(Node v) { create a queue Q enqueue v onto Q mark v “Visited” while Q is not empty { dequeue t from Q for each neighbor u of t do

if u is not marked { mark u “Visited” enqueue u onto Q

}}

Page 19: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Breadth First Search

L0

L1

L2

L3

Visit sequence: 1, 2, 3, 4, 5, 7, 8, 6

Page 20: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Applications: Finding a Path

Find path from source vertex s to destination vertex d Use graph search starting at s and terminating as soon as we

reach d Need to remember edges traversed Use depth – first search

Page 21: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

DFS

EF

G

B

CD

A start

destination

A DFS on A ADFS on BB

A

DFS on CBC

AB Return to call on BD Call DFS on D

ABD

Call DFS on GG found destination - done!Path is implicitly stored in DFS recursionPath is: A, B, D, G

DFS Process

Page 22: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Minimum spanning trees

Minimum spanning tree. Given a connected undirected graph G = (V, E) with real-valued edge weights ce, an MST is a subset of the edges T E such that T is a spanning tree whose sum of edge weights is minimized.

Page 23: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Minimum spanning trees

Page 24: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Greedy Algorithms

Prim's algorithm. Start with some root node s and greedily grow a tree T from s outward. At each step, add the cheapest edge e to T that has exactly one endpoint in T.

Theorem. Let T be the MST of G= (V, E), and let A⊆V. Suppose that (u, v) ∈ E is the least-weight edge connecting A to V–A. Then, (u, v) ∈T.

Page 25: Graphs Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.

Greedy Algorithms

Proof:• Suppose (u, v) ∉T.• Consider the unique simple path from u to v in T.• Swap (u, v) with the first edge on this path that connects a vertex in A to a vertex in V–A to get a lower-weight MST