elec1087: discrete mathematicswork1807/lec08-graph.pdf · elec1087: discrete mathematics lecture 8,...

18
ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations from Rosen, Graham et al., and wikipedia) Motivation Motivation ` In the city of Königsberg, Prussia, there were 7 bridges connecting two islands and the two river banks. ` Is there a way to walk through all 7 bridges without repeating the same bridge twice? C /di i h i ll ? C h ll? ` Can we prove/disprove it mathematically? Count them all? Euler proved in 1736 that it is not possible. His proof started the study of “graphs”. PAGE 2

Upload: others

Post on 25-Mar-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

ELEC1087: Discrete Mathematics

Lecture 8, 9: Graphs

Spring 2010 / Edmund Lam(based on notes by Dr Hayden So; illustrations from Rosen, Graham et al., and wikipedia)

MotivationMotivationIn the city of Königsberg, Prussia, there were 7 bridges connecting two islands and the two river banks.g

Is there a way to walk through all 7 bridges without repeating the same bridge twice?C /di i h i ll ? C h ll?Can we prove/disprove it mathematically? Count them all?

Euler proved in 1736 that it is not possible. pHis proof started the

study of “graphs”.

PAGE 2

Page 2: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

Graph Problems (in disguise)Graph Problems (in disguise)A company has 7 workers and 7 tasks. Each worker is trained to do some of the 7 tasks. How can we assign tasks to the workers such that all tasks will be handled by at least one person?What is the fastest way to get from HKU to Lo Wu?y gHow do we schedule the sub-tasks of a project to minimize the total project completion time?H can e artiti n a circ it int 2 ICs s as t minimi e How can we partition a circuit into 2 ICs so as to minimize the number of off-chip wires?How many colors do we need to color a map such that no two adjacent countries are filled with the same color?How many wireless network node may transmit at the same time without causing interference?g

PAGE 3

Graph theoryGraph theoryGoal:

Recognize that (some of) these problems can be solved Recognize that (some of) these problems can be solved (efficiently)Recognize that solving one may help solve othersFormalize a methodology to do so

Our roadmap:Our roadmap:“vocabulary”: basic terminologies“grammar”: basic manipulationsgrammar : basic manipulations“literature”: coloring, tree, shortest path…

PAGE 4

Page 3: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

Basic Graph TerminologiesBasic Graph TerminologiesA graph G=(V,E) is a collection of two entities

A non-empty set of vertices, VA set of edges, E

An edge connects either 1 gor 2 vertices

Si l hSimple graphNo loopNo multiple edge

Directed graphEach edge connects an ordered pair of vertices

PAGE 5

Basic Graph TerminologiesBasic Graph TerminologiesIf two vertices, u and v, are connected by an edge e in an undirected graph G, then we say:

u and v are adjacent,e is incident with the vertices u and v,u and v are endpoints of e,

The degree of a vertex in an undirected graph is the number of edges incident with it

A loop at a vertex contributes twice to the degree of that vertexA loop at a vertex contributes twice to the degree of that vertex.The degree of a node v denoted as deg(v)

F

A

B

ED

Fdeg(A) = 4 deg(B) = 3 deg(C) = 3

deg(D) = 4 deg(E) = 3 deg(F) = 3

PAGE 6

BC

Page 4: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

Basic Graph PropertiesBasic Graph PropertiesLet G = (V,E) be an undirected graph, then

2 E = deg(v)∑This is called the “handshaking theorem”.A di t d h G (V E) h b f

2 E = deg(v)v∈V

An undirected graph G=(V,E) has an even number of vertices of odd degrees.

Proof:Let V be the set of all vertices with even degrees and V be the Let V1 be the set of all vertices with even degrees and V2 be the set of all vertices with odd degrees, then

2 E = deg(v)∑ = deg(v)∑ + deg(v)∑Since the first term on the right is even, the second term on the right must also be even. Now since deg(v) is odd for all v’s in V2, there must be even number of vertices in V2.

v∈V v∈V1 v∈V2

2

PAGE 7

Basic Graph PropertiesBasic Graph Properties

In a directed graph,In degree of vertex v (deg+(v))is the number of edges with vIn-degree of vertex v (deg (v))is the number of edges with vas their terminal vertexOut-degree of vertex v (deg-(v))is the number of edges with v as their initial vertex

In a directed graph G=(V, E),

E = deg−(v)v∈V

∑ = deg+(v)v∈V

AE

D

Fdeg-(A) = 3 deg+(A) = 1

deg-(C) = 1 deg+(C) = 2A

BC

D deg (C) = 1 deg (C) = 2

PAGE 8

Page 5: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

Special Simple GraphsSpecial Simple GraphsComplete Graph (Kn)

All vertices are connected K1 K2 K3

to every other vertices with exactly 1 edge

Cycles (C )

K4 K5

Cycles (Cn)V={v1,v2,…,vn} and E={[v1,v2], [v2,v3],…,[vn-1,vn], [vn,v1]}n 1 n n 1 }

n-cubes (Qn)Vertices represent the 2n

bi i f l h

C3 C4 C5

110 111

bitstrings of length nTwo vertices adjacent iff their bitstring representations differ by

100

011

001000

010101

representations differ by exactly 1 position

PAGE 9

Q1 Q2 Q3

001000

Bipartite GraphBipartite GraphA simple graph G=(V,E) is called bipartite if V can be partitioned into V1 V2two disjoint sets V1 and V2 such that every edge in E connects a vertex in V1 and a vertex in V2

1 V2

A simple graph is bipartite if and only if it is possible to color its vertices such that no two adjacent vertices

bipartite

jare of same colorComplete Bipartite Graph (Km,n)

2 disjoint sets of vertices V and V with not bipartite2 disjoint sets of vertices V1 and V2 with m and n nodes respectivelyEach node in V1 is adjacent only to all nodes in V2 K3 4

not bipartite

V2

PAGE 10

K3,4

Page 6: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

Subgraphs and UnionSubgraphs and UnionA subgraph of a graph G=(V,E) is a graph H=(W,F) where W⊆ V and F ⊆ E⊆ ⊆

AE

D

F

B

ED

subgraph

The union of two simple graphs G1=(V1,E1) and G2=(V2,E2) is th h G (V E) h V V ∪V d E E ∪E

BC

BC

the graph G=(V,E) where V=V1∪V2 and E=E1∪E2

F

AE

D

F

A

B

ED

F

AE

D

F

+ =B

CB

CBC

PAGE 11

ConnectivityConnectivityAn undirected graph is connected if there is a path between every pair of distinct vertices of the graph.A connected component of a graph G is a connected subgraph of G that is not a proper subgraph of another connected subgraph of G.

i.e. a connected component is a maximal connected subgraphe.g. G1, G2

A graph G that is not connected has two or more A graph G that is not connected has two or more connected components, G1, G2…

G1, G2,… are disjointThe union of G1 G2 is G

G1G

The union of G1, G2,… is G.

G2

PAGE 12

Page 7: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

Path and CircuitPath and CircuitA path is a sequence of edges e1,e2,…, en that e1 connects {v0,v1}, e2 connects {v1,v2}… en connects {vn-1,vn}In a simple graph, a path can be uniquely defined by the vertices that the path passes through.A path is called a circuit if it begins and ends at the same p gvertex and has a length longer than zero.A path is simple if it does not contain the same edge more than once.more than once.

PAGE 13

Euler Paths and CircuitsEuler Paths and CircuitsRecall the problem of the city of Königsberg:

The solution is to determine if there exists an Euler circuit in the graph that represents the map of the city using an edge to represent a bridge.

An Euler circuit in a graph G is a simple circuit An Euler circuit in a graph G is a simple circuit containing every edge of GAn Euler path is a graph G is a simple path p g p p pcontaining every edge of G

PAGE 14

Page 8: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

Euler Paths and CircuitsEuler Paths and CircuitsHow can we know if Euler path/circuit exists in a graph?A connected multigraph with at least two vertices has an Euler circuit if and only if each of its vertices h dhas even degree.A connected multigraph has an Euler path but not an Euler circuit if and only if it has exactly two an Euler circuit if and only if it has exactly two vertices of odd degree.How about the city of Königsberg?

PAGE 15

Hamilton Paths and CircuitsHamilton Paths and CircuitsEuler is concerned with edges; Hamilton with vertices.A Hamilton path (circuit) in a graph G is a simple path A Hamilton path (circuit) in a graph G is a simple path (circuit) that passes through every vertex exactly once.Example:

Hamilton’s original problem: the icosian game

PAGE 16

Page 9: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

Hamilton Paths and CircuitsHamilton Paths and CircuitsIs there a way to test if a graph has a Hamilton path/circuit?p

Ans: in some case, yes…[Dirac] If deg(v) ≥ |V|/2 for all vertices, then G has a Hamilton circ itHamilton circuit[Ore] If deg(u)+deg(v) ≥ |V| for every pair of nonadjacent vertices u, v in G, then G has a Hamilton circuit

Is there an easy way to find a Hamilton circuit in graph?

Ans: NAns: NoBest algorithms known for finding a Hamilton circuit in a graph have exponential worst-time complexity.NP-complete problem

PAGE 17

Erdös graphErdös graphCollaboration graph of Paul Erdös (who wrote >1400 papers)

Erdös number 0 --- 1 personErdös number 1 --- 504 peopleErdös number 2 --- 6593 peopleErdös number 3 --- 33605 peopleErdös number 4 --- 83642 peopleErdös number 5 --- 87760 peopleErdös number 6 --- 40014 peopleErdös number 7 --- 11591 peopleErdös number 8 --- 3146 peopleErdös number 9 --- 819 peopleErdös number 10 244 peopleErdös number 10 --- 244 peopleErdös number 11 --- 68 peopleErdös number 12 --- 23 peopleErdös number 13 --- 5 people

http://www.oakland.edu/enp/trivia/

My Erdös number is… 4?

PAGE 18

Page 10: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

Hollywood graphHollywood graphCollaborative graph of movie actors

http://www.math.ucsd.edu/~fan/graphs/gallery/holy.jpgp g p g y y jpg

PAGE 19

Worth thinking…Worth thinking…

Textbook graphs are simplistic, real life graphs are complex But we have a way to graphs are complex. But we have a way to

represent these graphs (say, in the computer).

PAGE 20

Page 11: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

IsomorphismIsomorphismTwo graphs may look very different, but they are in fact the same.Wh t h h th m t f ti When two graphs have the same set of vertices connected in the same way, then they are isomorphicTwo graphs G(V,E) and H(W,F) are isomorphic if and

l if th i t t ti f W = { ( ) ( ) only if there exist a permutation of W = {p(v1), p(v2)… p(vn)} and (p(v1), p(v2)) is in F if and only if (v1, v2) is in E.

i.e. after twisting and stretching and “massage”, one can morph to anothermorph to another

PAGE 21

Planar graphPlanar graphA graph that can be drawn in a 2D space without any edge crossing each other is called a planar graph.For example, K3, K4 is planar, but K5 is not.Graphs that don’t look like planar can actually be planar isomorphismplanar -- isomorphism

K3

K4

K5

PAGE 22

Page 12: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

Euler Formula for Planar GraphsEuler Formula for Planar Graphs

vertexedge

region

edge

Euler derived the relationship between the b f d (E) ti (V) d f

region

number of edge (E) , vertices (V) and faces (regions) (F) for a finite, connected, and planar graphg p

V - E + F = 2

PAGE 23

Graph ColoringGraph ColoringThe task of labeling each vertex, edge, or face with a color subject to certain constraints.jMost of the time we talk about vertex coloring

Edge/face coloring problems can be transformed into a t l i blvertex coloring problem

In a vertex coloring problem, the vertices of a graph is labeled with colors such that no two adjacent vertices jhave the same color.The minimum number needed for such coloring is

ll d h h ti b f hcalled the chromatic number of a graph.

PAGE 24

Page 13: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

Graph Coloring ExamplesGraph Coloring Examples

K4 K5

K3,4

Some interesting results:Complete graph K is always n-coloredComplete graph Kn is always n-coloredBipartite graphs are always 2-coloredHow about Cycles? Cubes? Wheels?

PAGE 25

Face ColoringFace ColoringColor the faces of a planar graph with unique colors such that no two adjacent faces share the same color.T l t d i t t l i bl m th h it Translated into a vertex coloring problem through its planar dual.Face coloring originated from map coloring:

C ff Color each country with different color

PAGE 26

Page 14: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

Four Color TheoremFour Color TheoremWhat is the minimum number color needed to color different countries on a map so that no adjacent country shares the same color?adjacent country shares the same color?Ans: 4Precisely, the four color theorem states that:y,“Every planar graph is four-colorable”

PAGE 27

Four Color TheoremFour Color Theorem

Most of the time 3 is enough, but sometimes, 4 gis needed (when?)It was not proven until 2002!p

Used help of supercomputerProof that none of the counter example exists

PAGE 28

Page 15: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

Try thisTry this

PAGE 29

TreeTreeTree is an important class of graphA tree is a connected undirected graph with no simple circuitsEach node has exactly 1 parent and 1 or more children except Each node has exactly 1 parent, and 1 or more children, except the root node, which does not have a parent.A node without any children is called a leave node

rootroot

node

leave

PAGE 30

Page 16: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

Tree PropertiesTree PropertiesAn undirected graph is a tree if and only if there is a unique simple path between any two f i iof its vertices

A tree with n vertices has n-1 edges

PAGE 31

Binary TreeBinary TreeIf all the nodes of a tree have at most 2 children, then it is called a binary tree.I l if ll h d f h In general, if all the nodes of a tree have at most mchildren, then it is called a m-ary tree. An m-ary tree with i internal vertices contains n = ymi+1 vertices.

3-ary tree Binary treePAGE 32

Page 17: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

Shortest PathShortest PathIn many cases, we want to find the shortest path between two nodes in a graph:

Traffic routingTraffic routingInternet routingJob scheduling…

E h d i i i ht/ tEach edge is given a weight/costCommonly use Dijkstra’s shortest path algorithm

6

14

158

2

9

3

7

10

PAGE 33

Dijkstra’s Shortest Path AlgorithmDijkstra s Shortest Path AlgorithmUse a labeling scheme to find the shortest path from s to t.from s to t.Initially all nodes labeled with infinity, except s is labeled with 0.

while t is not markedfind the node (u) with minimum labelthat is not markedthat is not marked

mark the node ufor all vertices v that is not marked

( ) i ( ( ) ( ) ( ))L(v) = min( L(u) + w(u,v), L(v))L(t) is the shortest path from s to t.

PAGE 34

Page 18: ELEC1087: Discrete Mathematicswork1807/lec08-graph.pdf · ELEC1087: Discrete Mathematics Lecture 8, 9: Graphs Spring 2010 / Edmund Lam (based on notes by Dr Hayden So; illustrations

6

14

15

15 inf

6

14

15

11 10

6

2

9

15

3

8 70inf

6

2

9

15

3

8 70inf

103 inf 103 13

1411 101411 10

6

2

9

15

3

8 7016

615

2

9

3

8 7016

23

103 13

23

103 13

1411 10 1411 10

6

9

14

15

8 70

11 10

15

6

9

14

15

8 70

11 10

15

PAGE 35

23

103 13

23

103 13