64
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Embed Size (px)

Citation preview

Page 1: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Data Structures & Algorithms

Graphs

Richard Newmanbased on book by R. Sedgewick

and slides by S. Sahni

Page 2: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Definitions

• G = (V,E)• V is the vertex set• Vertices are also called nodes• E is the edge set• Each edge connects two different

vertices. • Edges are also called arcs

Page 3: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Definitions

• Directed edge has an orientation (u,v)

• Undirected edge has no orientation {u,v}

• Undirected graph => no oriented edge

• Directed graph (a.k.a. digraph) => every edge has an orientation

u v

u v

Page 4: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

(Undirected) Graph

23

8101

45

911

67

Page 5: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Directed Graph

23

8101

45

911

67

Page 6: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Application: Communication NW

Vertex = city, edge = communication link

23

8101

45

911

67

Page 7: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Driving Distance/Time Map

Vertex = cityedge weight = driving distance/time

23

8101

45

911

67

48

6

6

7

5

24

4 53

Page 8: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Street Map2

3

8101

45

911

67

Some streets are one way.

Page 9: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Complete Undirected Graph

Has all possible edges.

n = 1 n = 2 n = 3 n = 4

Also known as clique

Page 10: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Number Of Edges—Undirected Graph

• Each edge is of the form (u,v), u != v• Number of such pairs in an n = |V|

vertex graph is n(n-1)• Since edge (u,v) is the same as edge

(v,u), the number of edges in a complete undirected graph is n(n-1)/2

• Number of edges in an undirected graph is |E| <= n(n-1)/2

Page 11: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Number Of Edges—Directed Graph

• Each edge is of the form (u,v), u != v• Number of such pairs in an n = |V|

vertex graph is n(n-1)• Since edge (u,v) is NOT the same as

edge (v,u), the number of edges in a complete directed graph is n(n-1)

• Number of edges in a directed graph is |E| <= n(n-1)

Page 12: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Vertex Degree

Number of edges incident to vertex.

degree(2) = 2, degree(5) = 3, degree(3) = 1

23

8101

45

911

67

Page 13: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Sum Of Vertex Degrees

Sum of degrees = 2e (e is number of edges)

810

911

Page 14: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

In-Degree Of A Vertex

in-degree is number of incoming edges

indegree(2) = 1, indegree(8) = 0

23

8101

45

911

67

Page 15: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Out-Degree Of A Vertex

out-degree is number of outbound edges

outdegree(2) = 1, outdegree(8) = 2

23

8101

45

911

67

Page 16: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Sum Of In- And Out-Degrees

• each edge contributes 1 to the in-degree of some vertex and 1 to the out-degree of some other vertex

• sum of in-degrees = sum of out-degrees = e,

• where e = |E| is the number of edges in the digraph

Page 17: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Sample Graph Problems

• Path problems• Connectedness problems• Spanning tree problems• Flow problems• Coloring problems

Page 18: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Path FindingPath between 1 and 8

23

8101

45

911

67

48

6

6

7

5

24

43

Path length is 20

Page 19: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Path FindingAnother path between 1 and 8

Path length is 28

23

8101

45

911

67

48

6

6

7

5

24

43

Page 20: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Path FindingNo path between 1 and 10

23

8101

45

911

67

48

6

6

7

5

24

43

Page 21: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Connected Graph

• Undirected graph• There is a path between every pair of

vertices

Page 22: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Example of Not Connected

23

8101

45

911

67

48

6

6

7

5

24

43

Page 23: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Example of Connected

23

8101

45

911

67

48

6

6

7

5

24

43

5

Page 24: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Connected Component

• A maximal subgraph that is connected

• Cannot add vertices and edges from original graph and retain connectedness

• A connected graph has exactly 1 component

Page 25: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Connected Components

23

8101

45

911

67

Page 26: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Communication Network

Each edge is a link that can be constructed (i.e., a feasible link)

23

8101

45

911

67

Page 27: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Communication Network Problems

• Is the network connected?• Can we communicate between every

pair of cities?• Find the components• Want to construct smallest number of

feasible links so that resulting network is connected

Page 28: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Cycles And Connectedness2

3

8101

45

911

67

Removal of an edge that is on a cycle does not affect connectedness.

Page 29: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Cycles And Connectedness2

3

8101

45

911

67

Connected subgraph with all vertices and minimum number of edges has no cycles

Page 30: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Tree

• Connected graph that has no cycles• n vertex connected graph with n-1

edges

Page 31: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Spanning Tree

• Subgraph that includes all vertices of the original graph

• Subgraph is a tree• If original graph has n vertices, the

spanning tree has n vertices and n-1 edges

Page 32: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Minimum Cost Spanning Tree

Tree cost is sum of edge weights/costs

23

8101

45

911

67

48

6

6

7

5

24

4 53

8

2

Page 33: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

A Spanning Tree

Spanning tree cost = 51

23

8101

45

911

67

48

6

6

7

5

24

4 53

8

2

Page 34: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Minimum Cost Spanning Tree

Spanning tree cost = 41.

23

8101

45

911

67

48

6

6

7

5

24

4 53

8

2

Page 35: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

A Wireless Broadcast Tree

Source = 1, weights = needed power.Cost = 4 + 8 + 5 + 6 + 7 + 8 + 3 = 41

23

8101

45

911

67

48

6

6

7

5

24

4 53

8

2

Page 36: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Graph Representation

• Adjacency Matrix• Adjacency Lists

• Linked Adjacency Lists• Array Adjacency Lists

Page 37: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Adjacency Matrix

• Binary (0/1) n x n matrix, • where n = # of vertices• A(i,j) = 1 iff (i,j) is an edge

23

1

45

1 2 3 4 512345

0 1 0 1 01 0 0 0 10 0 0 0 11 0 0 0 10 1 1 1 0

Page 38: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Adjacency Matrix Properties

23

1

45

1 2 3 4 512345

0 1 0 1 01 0 0 0 10 0 0 0 11 0 0 0 10 1 1 1 0

• Diagonal entries are zero• Adjacency matrix of an undirected

graph is symmetric• A(i,j) = A(j,i) for all i and j

Page 39: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Adjacency Matrix (Digraph)

23

1

45

1 2 3 4 5

1

2

3

4

5

0 0 0 1 0

1 0 0 0 1

0 0 0 0 0

0 0 0 0 1

0 1 1 0 0

•Diagonal entries are zero

•Adjacency matrix of a digraph need not be symmetric.

Page 40: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Adjacency Matrix

• n2 bits of space• For an undirected graph, may store

only lower or upper triangle (exclude diagonal).

• Space? (n-1)n/2 bits• Time to find vertex degree and/or

vertices adjacent to a given vertex? O(n)

Page 41: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Adjacency Lists

Adjacency list for vertex i is a linear list of vertices adjacent from vertex i

Graph is an array of n adjacency lists

23

1

45

aList[1] = (2,4)

aList[2] = (1,5)

aList[3] = (5)

aList[4] = (5,1)

aList[5] = (2,4,3)

Page 42: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Linked Adjacency Lists

Each adjacency list is a chain.

23

1

45

aList[1]

aList[5]

[2]

[3]

[4]

2 4

1 5

5

5 1

2 4 3

Array Length = n# of chain nodes = 2e (undirected graph)# of chain nodes = e (digraph)

Page 43: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Array Adjacency Lists

Each adjacency list is an array list

23

1

45

aList[1]

aList[5]

[2]

[3]

[4]

2 4

1 5

5

5 1

2 4 3

Array Length = n# of list elements = 2e (undirected graph)# of list elements = e (digraph)

Page 44: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Weighted Graph Representations

• Cost adjacency matrix• C(i,j) = cost of edge (i,j)

• Adjacency lists • Each list element is a pair • (adjacent vertex, edge weight)

Page 45: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Paths

• Simple Path

• List of distinct vertices v0, v1, … , vn

• Each pair of successive vertices is an edge in E (vi, vi+1)

• Hamilton Path • Visit each node exactly once

• Euler Path• Visit each edge exactly once

Page 46: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Bipartite Graph

• Vertex set V can be partitioned into two disjoint subsets, V0 and V1

• No two vertices in Vi have an edge between them

Complete bipartite graphs

Page 47: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Hamilton PathDoes this graph have a Hamilton path?

23

8101

45

911

67

Yes! 2 or 7 to 10 Very hard in general

Page 48: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Euler PathDoes this graph have a Euler path?

23

8101

45

911

67

Yes! 9 to 10 Very easy in general

Page 49: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Euler Path• Bridges of Königsberg• People wanted to walk over all bridges

without crossing one twice

So they asked Euler …

Page 50: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Euler Path• Bridges of Königsberg• People wanted to walk over all bridges

without crossing one twice

Does this graph have a Euler path?

Page 51: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Euler Path

• Easy to determine existence• Graph must be connected• Degree of all nodes…• … must be even, except for two

• A little work to find the path• But also efficient…• Find path between odd nodes• Add loops as encountered

Page 52: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Graph Problems

• These two problems• Seem very similar• One is easy• The other is really hard!

• Classify graph problems• Easy• Tractable• Intractable• Unknown

Page 53: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Easy Graph Problems

• Simple, efficient algorithms exist• Linear or small polynomial time

• Simple connectivity• Strong connectivity in digraphs• Transitive closure• Minimum spanning tree• Single-source shortest path (SSSP)

Page 54: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Tractable Graph Problems

• Polynomial time algorithm is known… but

… hard to make into a practical program• Planarity

•Can graph be drawn without any lines representing edges intersecting?

• Matching•Largest subset of edges where no two connect to same vertex

• Even cycles in digraphs

Page 55: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Graph Planarity

• Kuratowski’s theorem:

For a graph to be non-planar

It must contain (after removal of degree-2 nodes) a subgraph isomorphic to either

6-node complete bipartite graph, or

5-clique

Page 56: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

More Tractable Graph Problems

• Often tractable problems can be solved with general purpose algorithm through graph transformation

• Assignment (network-flow)• Bipartite weighted matching – minimum

weight perfect matching in bipartite graph• Edge-connectivity (network-flow)

• What is minimum number of edges whose removal will partition graph?

• Node-connectivity (network-flow)

Page 57: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

More Tractable Graph Problems

• Mail carrier problem• Tour with minimum number of edges that

uses every edge at least once• Harder than Euler tour• Easier than Hamilton tour

Page 58: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Intractable Graph Problems

• No known polynomial time algorithm• NP-hard complexity class• However, may be polynomial time to

verify a solution (class NP)• Longest path (version of Hamilton Path)

• What is the longest simple path in G?• Independent Set

• Largest subset of vertices where no two have an edge between them

Page 59: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Intractable Graph Problems

• Colorability• Assign k colors to vertices such that no

edge connects two vertices of same color• Easy for k=2 (bipartite graph)

• Only even length cycles• Hard for k=3!

• Clique• What is largest complete subgraph?

(What is relationship to Independent Set?)

Page 60: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Graph Coloring

Can this graph be 2-colored?

No! Why not?

Odd cycle!

Can it be 3-colored?

Yes!

Page 61: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Graph Edge Coloring

Color each edge so that no two edges of same color are adjacent to same vertex

Chromatic Index = min # colors needed

Vizing’s Theorem: CI is either max degree or +1

Can it be 4-edge colored? Yes!

Page 62: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Graph Edge Coloring

• Despite the narrow range of possibilities• This problem is still intractable!!!!!

• There are polynomial time algorithms for bipartite graphs

• Brings up larger issue:• Can we solve exactly hard problem for

special case?• Can we “get close” for all cases?• How close is “close”?

Page 63: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Unknown Graph Problems

• Graph Isomorphism• Are two graphs identical other than the

names of their vertices?• Note that subgraph isomorphism is hard!

• How do you know this already?• Clique problem!

Page 64: Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni

Summary

• Graph definitions, properties

• Graph representations

• Graph problems and classifications

• Next: Graph Search, Digraphs, DAGs