cmsc 203 - discrete structures1 let us switch to a new topic: graphs

51
CMSC 203 - Discrete Structures 1 Let us switch to a new topic: Let us switch to a new topic: Graphs Graphs

Upload: janis-snow

Post on 23-Dec-2015

233 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 1

Let us switch to a new topic: Let us switch to a new topic:

GraphsGraphs

Page 2: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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.

Page 3: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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.

Page 4: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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

Page 5: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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

Page 6: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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}.

Page 7: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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).

Page 8: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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.

Page 9: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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

ddff 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).

Page 10: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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

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

Page 11: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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.

Page 12: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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

Page 13: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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.

Page 14: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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.

Page 15: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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

Page 16: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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.

Page 17: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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

Page 18: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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

Page 19: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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

Page 20: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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

Page 21: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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).

Page 22: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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:

Page 23: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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

Page 24: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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

Page 25: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 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

Page 26: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 26

Representing GraphsRepresenting Graphsaa

bb

cc

dd

aa

bb

cc

dd

a, da, dbb

a, da, dcc

a, b, ca, b, cdd

b, c, db, c, daa

Adjacent Adjacent VerticesVerticesVertexVertex

aabb

cc

a, b, ca, b, cdd

ccaa

Terminal Terminal VerticesVertices

Initial Initial VertexVertex

Page 27: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 27

Representing GraphsRepresenting Graphs

Definition:Definition: Let G = (V, E) be a simple graph with Let G = (V, E) be a simple graph with |V| = n. Suppose that the vertices of G are listed |V| = n. Suppose that the vertices of G are listed in arbitrary order as vin arbitrary order as v11, v, v22, …, v, …, vnn. .

The The adjacency matrixadjacency matrix A (or A A (or AGG) of G, with ) of G, with respect to this listing of the vertices, is the nrespect to this listing of the vertices, is the nn n zero-one matrix with 1 as its (i, j)th entry when vzero-one matrix with 1 as its (i, j)th entry when vii and vand vjj are adjacent, and 0 otherwise. are adjacent, and 0 otherwise.

In other words, for an adjacency matrix A = [aIn other words, for an adjacency matrix A = [aijij], ],

aaijij = 1 = 1 if {vif {vii, v, vjj} is an edge of G,} is an edge of G,aaijij = 0 = 0 otherwise.otherwise.

Page 28: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 28

Representing GraphsRepresenting Graphs

aa

bb

cc

dd

Example:Example: What is the What is the adjacency matrix Aadjacency matrix AGG for the for the following graph G based on following graph G based on the order of vertices a, b, c, the order of vertices a, b, c, d ?d ?

Solution:Solution:

0111

1001

1001

1110

GA

Note:Note: Adjacency matrices of undirected Adjacency matrices of undirected graphs are always symmetric.graphs are always symmetric.

Page 29: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 29

Representing GraphsRepresenting Graphs

Definition:Definition: Let G = (V, E) be an undirected Let G = (V, E) be an undirected graph with |V| = n. Suppose that the vertices and graph with |V| = n. Suppose that the vertices and edges of G are listed in arbitrary order as vedges of G are listed in arbitrary order as v11, v, v22, , …, v…, vn n and eand e11, e, e22, …, e, …, emm, respectively. , respectively.

The The incidence matrixincidence matrix of G with respect to this of G with respect to this listing of the vertices and edges is the nlisting of the vertices and edges is the nm zero-m zero-one matrix with 1 as its (i, j)th entry when edge one matrix with 1 as its (i, j)th entry when edge eejj is incident with v is incident with vii, and 0 otherwise., and 0 otherwise.

In other words, for an incidence matrix M = [mIn other words, for an incidence matrix M = [m ijij], ],

mmijij = 1 = 1 if edge eif edge ejj is incident with v is incident with vii mmijij = 0 = 0 otherwise.otherwise.

Page 30: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 30

Representing GraphsRepresenting GraphsExample:Example: What is the What is the incidence matrix M for the incidence matrix M for the following graph G based on following graph G based on the order of vertices a, b, c, d the order of vertices a, b, c, d and edges 1, 2, 3, 4, 5, 6?and edges 1, 2, 3, 4, 5, 6?

Solution:Solution:

001110

111000

000101

010011

M

Note:Note: Incidence matrices of directed graphs contain two 1s Incidence matrices of directed graphs contain two 1s per column for edges connecting two vertices and one 1 per per column for edges connecting two vertices and one 1 per column for loops.column for loops.

aa

bb

cc

dd

1122

445533

66

Page 31: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 31

Isomorphism of GraphsIsomorphism of Graphs

Definition:Definition: The simple graphs G The simple graphs G11 = (V = (V11, E, E11) and ) and GG22 = (V = (V22, E, E22) are ) are isomorphicisomorphic if there is a if there is a bijection (an one-to-one and onto function) f bijection (an one-to-one and onto function) f from Vfrom V11 to V to V22 with the property that a and b are with the property that a and b are adjacent in Gadjacent in G11 if and only if f(a) and f(b) are if and only if f(a) and f(b) are adjacent in Gadjacent in G22, for all a and b in V, for all a and b in V11..

Such a function f is called an Such a function f is called an isomorphismisomorphism..

In other words, GIn other words, G11 and G and G22 are isomorphic if their are isomorphic if their vertices can be ordered in such a way that the vertices can be ordered in such a way that the adjacency matrices Madjacency matrices MGG11

and M and MGG22 are identical. are identical.

Page 32: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 32

Isomorphism of GraphsIsomorphism of Graphs

From a visual standpoint, GFrom a visual standpoint, G11 and G and G22 are are isomorphic if they can be arranged in such a isomorphic if they can be arranged in such a way that their way that their displays are identicaldisplays are identical (of (of course without changing adjacency).course without changing adjacency).

Unfortunately, for two simple graphs, each with Unfortunately, for two simple graphs, each with n vertices, there are n vertices, there are n! possible n! possible isomorphismsisomorphisms that we have to check in order that we have to check in order to show that these graphs are isomorphic.to show that these graphs are isomorphic.

However, showing that two graphs are However, showing that two graphs are notnot isomorphic can be easy.isomorphic can be easy.

Page 33: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 33

Isomorphism of GraphsIsomorphism of GraphsFor this purpose we can check For this purpose we can check invariantsinvariants, that , that is, properties that two isomorphic simple graphs is, properties that two isomorphic simple graphs must both have.must both have.

For example, they must haveFor example, they must have

• the same number of vertices,the same number of vertices,

• the same number of edges, andthe same number of edges, and

• the same degrees of vertices.the same degrees of vertices.

Note that two graphs that Note that two graphs that differdiffer in any of these in any of these invariants are not isomorphic, but two graphs invariants are not isomorphic, but two graphs that that matchmatch in all of them are not necessarily in all of them are not necessarily isomorphic.isomorphic.

Page 34: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 34

Isomorphism of GraphsIsomorphism of GraphsExample I:Example I: Are the following two graphs isomorphic? Are the following two graphs isomorphic?

dd

aa

bb

cc

ee

dd

aa

bbcc

ee

Solution:Solution: YesYes, they are isomorphic, because they can be , they are isomorphic, because they can be arranged to look identical. You can see this if in the right graph arranged to look identical. You can see this if in the right graph you move vertex b to the left of the edge {a, c}. Then the you move vertex b to the left of the edge {a, c}. Then the isomorphism f from the left to the right graph is: f(a) = e, f(b) = a, isomorphism f from the left to the right graph is: f(a) = e, f(b) = a, f(c) = b, f(d) = c, f(e) = d. f(c) = b, f(d) = c, f(e) = d.

Page 35: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 35

Isomorphism of GraphsIsomorphism of GraphsExample II:Example II: How about these two graphs? How about these two graphs?

dd

aabb

cc

ee

dd

aa

bb

cc

ee

Solution:Solution: NoNo, they are not isomorphic, because , they are not isomorphic, because they differ in the degrees of their vertices.they differ in the degrees of their vertices.

Vertex d in right graph is of degree one, but there is Vertex d in right graph is of degree one, but there is no such vertex in the left graph.no such vertex in the left graph.

Page 36: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 36

ConnectivityConnectivity

Definition:Definition: A A pathpath of length n from u to v, of length n from u to v, where n is a positive integer, in an where n is a positive integer, in an undirected undirected graphgraph is a sequence of edges e is a sequence of edges e11, e, e22, …, e, …, enn of of the graph such that ethe graph such that e11 = {x = {x00, x, x11}, e}, e22 = {x = {x11, x, x22}, }, …, e…, enn = {x = {xn-1n-1, x, xnn}, where x}, where x00 = u and x = u and xnn = v. = v.

When the graph is simple, we denote this path When the graph is simple, we denote this path by its by its vertex sequencevertex sequence x x00, x, x11, …, x, …, xnn, since it , since it uniquely determines the path.uniquely determines the path.

The path is a The path is a circuitcircuit if it begins and ends at the if it begins and ends at the same vertex, that is, if u = v. same vertex, that is, if u = v.

Page 37: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 37

ConnectivityConnectivity

Definition (continued):Definition (continued): The path or circuit is The path or circuit is said to said to pass throughpass through or traverse x or traverse x11, x, x22, …, x, …, xn-1n-1. .

A path or circuit is A path or circuit is simplesimple if it does not contain if it does not contain the same edge more than once.the same edge more than once.

Page 38: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 38

ConnectivityConnectivity

Let us now look at something new:Let us now look at something new:

Definition:Definition: An undirected graph is called An undirected graph is called connectedconnected if there is a path between every if there is a path between every pair of distinct vertices in the graph.pair of distinct vertices in the graph.

For example, any two computers in a network For example, any two computers in a network can communicate if and only if the graph of this can communicate if and only if the graph of this network is connected.network is connected.

Note:Note: A graph consisting of only one vertex is A graph consisting of only one vertex is always connected, because it does not contain always connected, because it does not contain any pair of distinct vertices.any pair of distinct vertices.

Page 39: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 39

ConnectivityConnectivityExample:Example: Are the following graphs connected? Are the following graphs connected?

dd

aabb

cc

ee

Yes.Yes.

dd

aabb

ccee

No.No.

dd

aabb

cc

ee

Yes.Yes.

dd

aabb

ccee

ffNo.No.

Page 40: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 40

ConnectivityConnectivity

Definition:Definition: A graph that is not connected is A graph that is not connected is the union of two or more connected subgraphs, the union of two or more connected subgraphs, each pair of which has no vertex in common. each pair of which has no vertex in common. These disjoint connected subgraphs are called These disjoint connected subgraphs are called the the connected componentsconnected components of the graph. of the graph.

Page 41: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 41

ConnectivityConnectivity

Example:Example: What are the connected components What are the connected components in the following graph?in the following graph?

aa

bb cc

ddff hh

ggjjff

ee

Solution:Solution: The connected components are the graphs The connected components are the graphs with vertices {a, b, c, d}, {e}, {f}, {f, g, h, j}.with vertices {a, b, c, d}, {e}, {f}, {f, g, h, j}.

Page 42: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 42

ConnectivityConnectivity

Definition:Definition: An directed graph is An directed graph is strongly strongly connectedconnected if there is a path from a to b and if there is a path from a to b and from b to a whenever a and b are vertices in from b to a whenever a and b are vertices in the graph. the graph.

Definition:Definition: An directed graph is An directed graph is weakly weakly connectedconnected if there is a path between any two if there is a path between any two vertices in the underlying undirected graph. vertices in the underlying undirected graph.

Page 43: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 43

ConnectivityConnectivityExample:Example: Are the following directed graphs Are the following directed graphs strongly or weakly connected?strongly or weakly connected?

aa

bb

cc

dd

Weakly connectedWeakly connected, , because, for example, there because, for example, there is no path from b to d.is no path from b to d.

aa

bb

cc

dd

Strongly connectedStrongly connected, , because there are paths because there are paths between all possible pairs of between all possible pairs of vertices.vertices.

Page 44: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 44

Shortest Path ProblemsShortest Path ProblemsWe can assign weights to the edges of graphs, We can assign weights to the edges of graphs, for example to represent the distance between for example to represent the distance between cities in a railway network:cities in a railway network:

ChicagChicagoo

TorontTorontoo

New YorkNew York

BostonBoston

600600

700700

200200

650650

Page 45: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 45

Shortest Path ProblemsShortest Path ProblemsSuch weighted graphs can also be used to Such weighted graphs can also be used to model computer networks with response times model computer networks with response times or costs as weights.or costs as weights.

One of the most interesting questions that we One of the most interesting questions that we can investigate with such graphs is:can investigate with such graphs is:

What is the What is the shortest pathshortest path between two between two vertices in the graph, that is, the path with the vertices in the graph, that is, the path with the minimal sum of weightsminimal sum of weights along the way? along the way?

This corresponds to the shortest train This corresponds to the shortest train connection or the fastest connection in a connection or the fastest connection in a computer network.computer network.

Page 46: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 46

Dijkstra’s AlgorithmDijkstra’s Algorithm

Dijkstra’s algorithm is an iterative procedure Dijkstra’s algorithm is an iterative procedure that finds the shortest path between to that finds the shortest path between to vertices a and z in a weighted graph.vertices a and z in a weighted graph.

It proceeds by finding the length of the It proceeds by finding the length of the shortest path from a to successive vertices and shortest path from a to successive vertices and adding these vertices to a distinguished set of adding these vertices to a distinguished set of vertices S. vertices S.

The algorithm terminates once it reaches the The algorithm terminates once it reaches the vertex z.vertex z.

Page 47: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 47

The Traveling Salesman ProblemThe Traveling Salesman ProblemThe The traveling salesman problemtraveling salesman problem is one of the is one of the classical problems in computer science.classical problems in computer science.

A traveling salesman wants to visit a number of A traveling salesman wants to visit a number of cities and then return to his starting point. Of cities and then return to his starting point. Of course he wants to save time and energy, so he course he wants to save time and energy, so he wants to determine the wants to determine the shortest pathshortest path for his trip. for his trip.

We can represent the cities and the distances We can represent the cities and the distances between them by a weighted, complete, between them by a weighted, complete, undirected graph.undirected graph.

The problem then is to find the The problem then is to find the circuit of circuit of minimum total weight that visits each vertex minimum total weight that visits each vertex exactly oneexactly one..

Page 48: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 48

The Traveling Salesman ProblemThe Traveling Salesman ProblemExample:Example: What path would the traveling What path would the traveling salesman take to visit the following cities?salesman take to visit the following cities?

ChicagChicagoo

TorontTorontoo

New YorkNew York

BostonBoston

600600

700700

200200

650650 550550700700

Solution:Solution: The shortest path is Boston, New The shortest path is Boston, New York, Chicago, Toronto, Boston (2,000 miles).York, Chicago, Toronto, Boston (2,000 miles).

Page 49: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 49

The Traveling Salesman ProblemThe Traveling Salesman ProblemQuestion:Question: Given n vertices, how many different Given n vertices, how many different cycles Ccycles Cnn can we form by connecting these can we form by connecting these vertices with edges?vertices with edges?

Solution:Solution: We first choose a starting point. Then We first choose a starting point. Then we have (n – 1) choices for the second vertex in we have (n – 1) choices for the second vertex in the cycle, (n – 2) for the third one, and so on, so the cycle, (n – 2) for the third one, and so on, so there are (n – 1)! choices for the whole cycle.there are (n – 1)! choices for the whole cycle.

However, this number includes identical cycles However, this number includes identical cycles that were constructed in that were constructed in opposite directionsopposite directions. . Therefore, the actual number of different cycles Therefore, the actual number of different cycles CCnn is is (n – 1)!/2(n – 1)!/2..

Page 50: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 50

The Traveling Salesman ProblemThe Traveling Salesman Problem

Unfortunately, no algorithm solving the Unfortunately, no algorithm solving the traveling salesman problem with polynomial traveling salesman problem with polynomial worst-case time complexity has been devised worst-case time complexity has been devised yet.yet.

This means that for large numbers of vertices, This means that for large numbers of vertices, solving the traveling salesman problem is solving the traveling salesman problem is impractical.impractical.

In these cases, we can use efficient In these cases, we can use efficient approximation algorithmsapproximation algorithms that determine a that determine a path whose length may be slightly larger than path whose length may be slightly larger than the traveling salesman’s path, but the traveling salesman’s path, but

Page 51: CMSC 203 - Discrete Structures1 Let us switch to a new topic: Graphs

CMSC 203 - Discrete Structures 51

TheTheEndEnd