25
1 Graphs Graphs Chapters 9.1 and 9.2 Chapters 9.1 and 9.2 Based on slides by Y. Peng University of Maryland University of Maryland

1 Graphs Chapters 9.1 and 9.2 University of Maryland Chapters 9.1 and 9.2 Based on slides by Y. Peng University of Maryland

Embed Size (px)

Citation preview

1

GraphsGraphs

Chapters 9.1 and 9.2Chapters 9.1 and 9.2

Based on slides by Y. PengUniversity of MarylandUniversity of Maryland

2

Introduction to GraphsIntroduction to Graphs

Definition: Definition: A A simple graphsimple graph G = (V, E) G = (V, E) consists of V, a nonempty set of vertices, and E, consists of V, a nonempty set of vertices, and E, a set of a set of unordered pairsunordered pairs of of distinctdistinct elements elements of V called edges.of V called edges.

For each eFor each eE, e = {u, v} where u, v E, e = {u, v} where u, v V. V.

An undirected graph (not simple) may contain An undirected graph (not simple) may contain loops. An edge e is a loop if e = {u, u} for some loops. An edge e is a loop if e = {u, u} for some uuV.V.

3

Introduction to GraphsIntroduction to Graphs

Definition: Definition: A A directed graph directed graph G = (V, E) G = (V, E) consists of a set V of vertices and a set E of consists of a set V of vertices and a set E of edges that are ordered pairs of elements in V.edges that are ordered pairs of elements in V.

For each eFor each eE, e = (u, v) where u, v E, e = (u, v) where u, v V. V.

An edge e is a loop if e = (u, u) for some uAn edge e is a loop if e = (u, u) for some uV.V.

A simple graph is just like a directed graph, but A simple graph is just like a directed graph, but with no specified direction of its edges.with no specified direction of its edges.

4

Graph ModelsGraph ModelsExample I: Example I: How can we represent a network of How can we represent a network of (bi-directional) railways connecting a set of cities?(bi-directional) railways connecting a set of cities?

We should use a We should use a simple graphsimple graph with an edge {a, with an edge {a, b} indicating a direct train connection between b} indicating a direct train connection between cities a and b.cities a and b.

New YorkNew York

BostonBoston

WashingtonWashington

LLüübeckbeckTorontoToronto

HamburHamburgg

5

Graph ModelsGraph ModelsExample II: Example II: In a round-robin tournament, each In a round-robin tournament, each team plays against each other team exactly once. team plays against each other team exactly once. How can we represent the results of the How can we represent the results of the tournament (which team beats which other team)?tournament (which team beats which other team)?

We should use a We should use a directed graphdirected graph with an edge (a, with an edge (a, b) indicating that team a beats team b.b) indicating that team a beats team b.

PenguinPenguinss

BruinsBruins

LLüübeck Giantsbeck Giants

Maple LeafsMaple Leafs

6

Graph TerminologyGraph Terminology

Definition: Definition: Two vertices u and v in an Two vertices u and v in an undirected graph G are called undirected graph G are called adjacentadjacent (or (or neighborsneighbors) in G if {u, v} is an edge in G.) in G if {u, v} is an edge in G.

If e = {u, v}, the edge e is called If e = {u, v}, the edge e is called incident incident withwith the vertices u and v. The edge e is also the vertices u and v. The edge e is also said to said to connectconnect u and v. u and v.

The vertices u and v are called The vertices u and v are called endpointsendpoints of of the edge {u, v}.the edge {u, v}.

7

Graph TerminologyGraph Terminology

Definition: Definition: The The degreedegree of a vertex in an of a vertex in an undirected graph is the number of edges undirected graph is the number of edges incident with it, except that a loop at a vertex incident with it, except that a loop at a vertex contributes twice to the degree of that vertex.contributes twice to the degree of that vertex.

In other words, you can determine the degree In other words, you can determine the degree of a vertex in a displayed graph by of a vertex in a displayed graph by counting counting the linesthe lines that touch it. that touch it.

The degree of the vertex v is denoted by The degree of the vertex v is denoted by deg(v).deg(v).

8

Graph TerminologyGraph Terminology

A vertex of degree 0 is called A vertex of degree 0 is called isolatedisolated, since it , since it is not adjacent to any vertex. is not adjacent to any vertex.

Note:Note: A vertex with a A vertex with a looploop at it has at least at it has at least degree 2 and, by definition, is degree 2 and, by definition, is not isolatednot isolated, , even if it is not adjacent to any even if it is not adjacent to any otherother vertex. vertex.

A vertex of degree 1 is called A vertex of degree 1 is called pendantpendant. It is . It is adjacent to exactly one other vertex.adjacent to exactly one other vertex.

9

Graph TerminologyGraph Terminology

Example: Example: Which vertices in the following graph Which vertices in the following graph are isolated, which are pendant, and what is are isolated, which are pendant, and what is the maximum degree? What type of graph is it?the maximum degree? What type of graph is it?

aa

bb cc

ddii hh

ggjjff

ee

Solution: Solution: Vertex f is isolated, and vertices a, d and Vertex f is isolated, and vertices a, d and j are pendant. The maximum degree is deg(g) = 5. j are pendant. The maximum degree is deg(g) = 5. This graph is a pseudograph (undirected, loops).This graph is a pseudograph (undirected, loops).

10

Graph TerminologyGraph Terminology

Let us look at the same graph again and Let us look at the same graph again and determine the number of its edges and the sum determine the number of its edges and the sum of the degrees of all its vertices:of the degrees of all its vertices:

aa

bb cc

ddii hh

ggjjff

ee

Result: Result: There are 9 edges, and the sum of all There are 9 edges, and the sum of all degrees is 18. This is easy to explain: Each new edge degrees is 18. This is easy to explain: Each new edge increases the sum of degrees by exactly two.increases the sum of degrees by exactly two.

11

Graph TerminologyGraph TerminologyThe Handshaking Theorem:The Handshaking Theorem: Let G = (V, E) Let G = (V, E) be an undirected graph with e edges. Thenbe an undirected graph with e edges. Then

2e = 2e = vvVV deg(v) deg(v)

Example:Example: How many edges are there in a How many edges are there in a graph with 10 vertices, each of degree 6?graph with 10 vertices, each of degree 6?

Solution:Solution: The sum of the degrees of the The sum of the degrees of the vertices is 6vertices is 610 = 60. According to the 10 = 60. According to the Handshaking Theorem, it follows that 2e = 60, Handshaking Theorem, it follows that 2e = 60, so there are 30 edges.so there are 30 edges.

12

Graph TerminologyGraph TerminologyTheorem:Theorem: An undirected graph has an even number of vertices of odd degree. An undirected graph has an even number of vertices of odd degree.

Proof:Proof: Let V1 and V2Let V1 and V2 be the set of vertices of even and odd degrees, be the set of vertices of even and odd degrees, respectively (Thus V1 respectively (Thus V1 V2 = V2 = , and V1 , and V1 V2 = V). V2 = V).

Then by Handshaking theoremThen by Handshaking theorem

2|E| = 2|E| = vvVV deg(v) = deg(v) = vvV1V1 deg(v) + deg(v) + vvV2V2 deg(v) deg(v)

Since both 2|E| and Since both 2|E| and vvV1V1 deg(v) are even, deg(v) are even,

vvV2V2 deg(v) must be even. deg(v) must be even.

Since deg(v) if odd for all vSince deg(v) if odd for all vV2, |V2| must be even.V2, |V2| must be even.

QEDQED

13

Graph TerminologyGraph Terminology

Definition:Definition: When (u, v) is an edge of the graph When (u, v) is an edge of the graph G with directed edges, u is said to be G with directed edges, u is said to be adjacent adjacent toto v, and v is said to be v, and v is said to be adjacent fromadjacent from u. u.

The vertex u is called the The vertex u is called the initial vertexinitial vertex of (u, of (u, v), and v is called the v), and v is called the terminal vertexterminal vertex of (u, of (u, v).v).

The initial vertex and terminal vertex of a loop The initial vertex and terminal vertex of a loop are the same.are the same.

14

Graph TerminologyGraph TerminologyDefinition:Definition: In a graph with directed edges, the In a graph with directed edges, the in-in-degreedegree of a vertex v, denoted by of a vertex v, denoted by degdeg--(v)(v), is the , is the number of edges with v as their number of edges with v as their terminal vertexterminal vertex..

The The out-degreeout-degree of v, denoted by of v, denoted by degdeg++(v)(v), is the , is the number of edges with v as their initial vertex.number of edges with v as their initial vertex.

Question:Question: How does adding a loop to a vertex How does adding a loop to a vertex change the in-degree and out-degree of that change the in-degree and out-degree of that vertex?vertex?

Answer:Answer: It increases both the in-degree and the It increases both the in-degree and the out-degree by one.out-degree by one.

15

Graph TerminologyGraph TerminologyExample:Example: What are the in-degrees and out- What are the in-degrees and out-degrees of the vertices a, b, c, d in this graph:degrees of the vertices a, b, c, d in this graph:

aabb

ccdd

degdeg--(a) = 1(a) = 1

degdeg++(a) = 2(a) = 2degdeg--(b) = 4(b) = 4

degdeg++(b) = 2(b) = 2

degdeg--(d) = 2(d) = 2

degdeg++(d) = 1(d) = 1degdeg--(c) = 0(c) = 0

degdeg++(c) = 2(c) = 2

16

Graph TerminologyGraph Terminology

Theorem:Theorem: Let G = (V, E) be a graph with Let G = (V, E) be a graph with directed edges. Then:directed edges. Then:

vvVV deg deg--(v) = (v) = vvVV deg deg++(v) = |E|(v) = |E|

This is easy to see, because every new edge This is easy to see, because every new edge increases both the sum of in-degrees and the increases both the sum of in-degrees and the sum of out-degrees by one. sum of out-degrees by one.

17

Special GraphsSpecial Graphs

Definition:Definition: The The complete graphcomplete graph on n on n vertices, denoted by Kvertices, denoted by Knn, is the simple graph , is the simple graph that contains exactly one edge between each that contains exactly one edge between each pair of distinct vertices.pair of distinct vertices.

KK11 KK22 KK33 KK44 KK55

18

Special GraphsSpecial Graphs

Definition:Definition: The The cyclecycle C Cnn, n , n 3, consists of n 3, consists of n vertices vvertices v11, v, v22, …, v, …, vnn and edges {v and edges {v11, v, v22}, {v}, {v22, , vv33}, …, {v}, …, {vn-1n-1, v, vnn}, {v}, {vnn, v, v11}.}.

CC33 CC44 CC55 CC66

19

Special GraphsSpecial Graphs

Definition:Definition: We obtain the We obtain the wheelwheel W Wnn when we when we add an additional vertex to the cycle Cadd an additional vertex to the cycle Cnn, for n , for n 3, and connect this new vertex to each of the n 3, and connect this new vertex to each of the n vertices in Cvertices in Cnn by adding new edges. by adding new edges.

WW33 WW44 WW55 WW66

20

Special GraphsSpecial GraphsDefinition:Definition: The The n-cube, n-cube, denoted by Qdenoted by Qnn, is the , is the graph that has vertices representing the 2graph that has vertices representing the 2nn bit bit strings of length n. Two vertices are adjacent if strings of length n. Two vertices are adjacent if and only if the bit strings that they represent and only if the bit strings that they represent differ in exactly one bit position.differ in exactly one bit position.

QQ11 QQ22 QQ33

00 110000 0101

11111010

000000 001001

101101100100

010010 011011

111111110110

21

Special GraphsSpecial GraphsDefinition:Definition: A simple graph is called A simple graph is called bipartitebipartite if if its vertex set V can be partitioned into two its vertex set V can be partitioned into two disjoint nonempty sets Vdisjoint nonempty sets V11 and V and V22 such that every such that every edge in the graph connects a vertex in Vedge in the graph connects a vertex in V11 with a with a vertex in Vvertex in V22 (so that no edge in G connects (so that no edge in G connects either two vertices in Veither two vertices in V11 or two vertices in V or two vertices in V22).).

For example, consider a graph that represents For example, consider a graph that represents each person in a village by a vertex and each each person in a village by a vertex and each marriage by an edge.marriage by an edge.

This graph is This graph is bipartitebipartite, because each edge , because each edge connects a vertex in the connects a vertex in the subset of malessubset of males with with a vertex in the a vertex in the subset of femalessubset of females (if we think (if we think of traditional marriages).of traditional marriages).

22

Special GraphsSpecial Graphs

Example I:Example I: Is C Is C33 bipartite? bipartite?

vv11

vv22 vv33

No,No, because there is no way to partition the because there is no way to partition the vertices into two sets so that there are no vertices into two sets so that there are no edges with both endpoints in the same set.edges with both endpoints in the same set.

Example II:Example II: Is C Is C66 bipartite? bipartite?

vv55

vv11

vv22

vv33 vv44

vv66vv11 vv66

vv22vv55

vv33vv44

Yes, Yes, because because we can display we can display CC66 like this: like this:

23

Special GraphsSpecial Graphs

Definition:Definition: The The complete bipartite graphcomplete bipartite graph KKm,nm,n is the graph that has its vertex set is the graph that has its vertex set partitioned into two subsets of m and n vertices, partitioned into two subsets of m and n vertices, respectively. Two vertices are connected if and respectively. Two vertices are connected if and only if they are in different subsets.only if they are in different subsets.

KK3,23,2 KK3,43,4

24

Operations on GraphsOperations on Graphs

Definition:Definition: A A subgraphsubgraph of a graph G = (V, E) of a graph G = (V, E) is a graph H = (W, F) where Wis a graph H = (W, F) where WV and FV and FE.E.

Note:Note: Of course, H is a valid graph, so we Of course, H is a valid graph, so we cannot remove any endpoints of remaining cannot remove any endpoints of remaining edges when creating H.edges when creating H.

Example:Example:

KK55 subgraph of subgraph of KK55

25

Operations on GraphsOperations on Graphs

Definition:Definition: The The unionunion of two simple graphs G of two simple graphs G11 = = (V(V11, E, E11) and G) and G22 = (V = (V22, E, E22) is the simple graph ) is the simple graph with vertex set Vwith vertex set V11 V V22 and edge set E and edge set E11 E E22. .

The union of GThe union of G11 and G and G22 is denoted by is denoted by GG11 G G22..

GG11 GG22 GG1 1 GG2 2 = = KK55