88
CS 103 Discrete Structures Lecture 20 Graphs (1)

CS 103 Discrete Structures Lecture 20 Graphs (1)

Embed Size (px)

Citation preview

Induction and recursion

CS 103 Discrete Structures Lecture 20Graphs (1)

1Chapter 1 section 1.1 by Dr. Mosaad HassanSecond Midterm Exam1st Lecture in December (same time as the lecture)75 minute durationWill cover all lectures delivered before the exam dateWill consist of MCQs, fill-in-the-blanks, questions with short answers, writing of proofs, and drawing of diagrams If you miss this exam for any reason, you will have to appear for a makeup exam on the Thursday of the last week of teaching. That exam will cover all lectures delivered in the semester. It will consist of writing of proofs, drawing of diagrams and answering questions having 0.5-1 page answers.2GraphsChapter 103Chapter SummaryGraphs and Graph ModelsGraph Terminology and Special Types of GraphsRepresenting Graphs and Graph IsomorphismConnectivity

4Graphs and Graph ModelsSection 10.15Section SummaryIntroduction to GraphsGraph TaxonomyGraph Models6GraphsA graph G = (V, E) consists of V, a nonempty set of vertices (or nodes), and E, a set of edges

Each edge has either 1 or 2 vertices associated with it, called its endpoints. An edge is said to connect its endpoints

A graph with an infinite V (i.e. consisting of an infinite number of vertices) is called an infinite graph

A graph with a finite V is called a finite graph. We will focus on finite graphs7Graphs: ExampleSuppose that a network is made up of data centers and communication links between computers. We can represent the location of each data center by a vertex and each communications link by an edgeSan FranciscoLos AngelesDenverNew YorkChicagoWashingtonDetroit How many vertices? How many edges?Note: We have a lot of freedom when we draw a graph. All that matters is the connections made by the edges, not the particular geometry depicted. For example, the lengths of edges, whether edges cross, how vertices are depicted, and so on, do not matter8Graphs: Example (contd.)Another way of depicting the graph on the previous slide:

V = {Chicago, Denver, Detroit, Los Angeles, New York,San Francisco, Washington}

E = {{San Francisco, Los Angeles}, {San Francisco, Denver},{Los Angeles, Denver}, {Denver, Chicago},{Chicago, Detroit}, {Detroit, New York},{New York, Washington}, {Chicago, Washington},{Chicago, New York}}

9Simple GraphsA graph is a simple graph ifeach edge connects 2 different verticesno 2 edges connect the same pair of vertices

In a simple graph each edge is associated with an unordered pair of vertices and no other edge is associated with that pairSan FranciscoLos AngelesDenverNew YorkChicagoWashingtonDetroit10MultigraphsGraphs that may have multiple edges connecting the same pair of vertices are called multigraphsSan FranciscoDenverLos AngelesNew YorkChicagoWashingtonDetroit11Pseudographs Pseudographs are graphs that include loops

Loops are edges that connect a vertex to itself

Pseudographs may include multiple edges as wellSan FranciscoDenverLos AngelesNew YorkChicagoWashingtonDetroit12TypeMultiple Edges Allowed?Loops Allowed?Simple graphsNoNoMultigraphsYesNoPseudographsYesYes13Undirected GraphsThe graphs we have introduced so far are undirected graphs

Their edges are undirected, i.e. do not include arrowheads. Each edge is associated with an unordered pair of verticesSan FranciscoDenverLos AngelesNew YorkChicagoWashingtonDetroit14Directed GraphsA directed graph (or digraph) G(V, E) consists of a nonempty set of vertices V and a set of directed edges (or arcs) E

Each directed edge is associated with an ordered pair of vertices. The directed edge associated with the ordered pair (u, v) is said to start at vertex u and end at vertex vDetroitSan FranciscoDenverLos AngelesNew YorkChicagoWashington15Simple Directed GraphWhen a directed graph has no loops or multiple directed edges, it is called a simple directed graph16Directed Multigraphs

When a directed graph includes multiple directed edges or loops, it is called a directed multigraph

When there are m directed edges, each associated to an ordered pair of vertices (u, v), we say that (u, v) is an edge of multiplicity m

17Mixed GraphA graph with both directed and undirected edges is called a mixed graph18Graph Terminology: SummaryTo understand the structure of a graph, we ask these questions:Are the edges of undirected or directed (or both)?If the edges are undirected, are multiple edges present that connect the same pair of vertices? If the edges are directed, are multiple directed edges present?Are loops present?

19Graph Models: Computer NetworksWhen we build a graph model, we use the appropriate type of graph to capture the important features of the applicationWe illustrate this process using graph models of different types of computer networks. In these models, the vertices represent data centers and the edges represent communication linksTo model a computer network where we are only concerned whether two data centers are connected by a communications link, we use a simple graph. This is the appropriate type of graph when we only carewhether two data centersare directly linked (andnot how many links theremay be) and all links workin both directions

20Graph Models: Computer Networks

To model a computer network where we care about the number of links between data centers, we use a multigraphTo model a computer network with diagnostic links at data centers, we use a pseudograph, as loops are needed

To model a network with multiple one-way links, we use a directed multigraph21Other Applications of GraphsWe will illustrate how graph theory can be used in models of:Social networksCommunications networksInformation networksSoftware designTransportation networksIts a challenge to find a subject to which graph theory has not yet been applied

22

Graph Models: Social NetworksGraphs can be used to model social structures based on different kinds of relationships between people or groupsIn a social network, vertices represent individuals or organizations and edges represent their relationshipsUseful graph models of social networks include:Friendship Graphs - undirected graphs where two people are connected if they are friends (in the real world, on Facebook, or in a particular virtual world, and so on.)

Influence Graphs - directed graphs where there is an edge from one person to another if the 1st person can influence the 2nd person

23Applications to Information Networks Graphs can be used to model different types of networks that link different types of information

For example, in a web graph, web pages are represented by vertices and links are represented by directed edgesA web graph models the web at a particular timeMore on how the web graph is used by search engines is discussed in Section 11.4

24Transportation GraphsGraph models are extensively used in the study of transportation networks.Airline networks can be modeled using directed multigraphs whereairports are represented by verticeseach flight is represented by a directed edge from the vertex representing the departure airport to the vertex representing the destination airportRoad networks can be modeled using graphs wherevertices represent intersections and edges represent roadsundirected edges represent two-way roads and directed edges represent one-way roads25Software Design ApplicationsGraph models are extensively used in software design. We will introduce here the model representing the dependency between the modules of a software application In software design, the system is divided into modules, each performing a specific task. We use a module dependency graph to represent the dependency between these modules. These dependencies need to be understood before coding can be done. In a module dependencygraph vertices representsoftware modules andthere is an edge fromone module to another ifthe second moduledepends on the first

26Graph Terminology and Special Types of GraphsSection 10.227Section SummaryBasic TerminologySome Special Types of GraphsBipartite GraphsBipartite Graphs and Matchings Some Applications of Special Types of GraphsNew Graphs from Old

28Graph TerminologyTwo vertices u and v in an undirected graph G are called adjacent in G if u and v are endpoints of an edge of GIf e is associated with {u, v}, the edge e is called incident with the vertices u and v. The edge e is also said to connect u and vThe degree of a vertex in an undirected graph is the number of edges incident with it, except that a loop at a vertex contributes twice to the degree of that vertexThe degree of the vertex v is denoted by deg(v)29Degree of a Vertex: Examplecabcdefdeg(a) = 2deg(b) = deg(c) = deg(f) = 4deg(d) = 1deg(e) = 3deg(g) = 0deg(a) = 4deg(b) = deg(e) = 6deg(c) = 1deg(d) = 5

eg30The Handshaking Theorem31The Handshaking TheoremExample 2 : If a graph has 5 vertices, can each vertex have degree 3?This is not possible by the handshaking theorem, because the sum of the degrees of the vertices 3 5 = 15 is odd

Theorem 2: An undirected graph has an even number of vertices of odd degree32Directed Graph TerminologyWhen (u, v) is a directed edge, u is said to be adjacent to v and v is said to be adjacent from uVertex u is called the initial or start vertex of (u, v)Vertex v is called the terminal or end vertex of (u, v)The start vertex and end vertex of a loop are the same33In-degree and Out-degreeIn a directed graph the in-degree of a vertex v, denoted by deg-(v), is the number of edges with v as their start vertexThe out-degree of v, denoted by deg+(v), is the number of edges with v as their end vertexA loop at a vertex contributes 1 to both the in-degree and the out-degree of this vertexTheorem: Let G = (V, E) be a graph with directed edges. Then

34In-degree and Out-degree: Exampledeg-(a) = 2, deg-(b) = 2deg-(c) = 3, deg-(d) = 2deg-(e) = 3, deg-(f) = 0

deg+(a) = 4, deg+(b) = 1deg+(c) = 2, deg+(d) = 2deg+(e) = 3, deg+(f) = 0

f35Complete GraphsThe complete graph on n vertices, Kn , is the simple graph that contains exactly 1 edge between each pair of distinct vertices, i.e. every node is adjacent to every other nodeu V, v V : u v {u, v}E

Kn has n vertices and n(n - 1)/2 edges

36CyclesThe cycle Cn , n 3, consists of n vertices v1 , v2 , . . . , vn and edges {v1 , v2}, {v2 , v3}, , {vn-1, vn}, and {vn , v1}

Cn has n vertices and n edges

37WheelsWe obtain the wheel Wn when we add an additional vertex to the cycle Cn , n 3, and connect this new vertex to each of the n vertices in Cn , by new edges

Wn has n vertices and 2n edges

38n-CubesThe n-dimensional hypercube, or n-cube, denoted by Qn is the graph that has vertices representing the 2n-bit strings of length n. Vertices are adjacent iff the bit strings that they represent differ in exactly 1 bit position

Q1 Q2 Q3

Qn has 2n vertices and n2n - 1 edges

110100100111011110010100000101001139Bipartite GraphsA simple graph G is bipartite if V can be partitioned into two disjoint subsets V1 and V2 such that every edge connects a vertex in V1 and a vertex in V2. In other words, there are no edges which connect two vertices in V1 or in V2

40Complete Bipartite GraphsA complete bipartite graph Km,n is a graph that has its vertex set partitioned into two subsets V1 of size m and V2 of size n such that there is an edge from every vertex in V1 to every vertex in V2

Km,n has m + n vertices and m n edges

41CS 103 Discrete Structures Lecture 21Graphs (2)

42Chapter 1 section 1.1 by Dr. Mosaad HassanSecond Midterm Exam1st Lecture in December (same time as the lecture)75 minute durationWill cover all lectures delivered before the exam dateWill consist of MCQs, fill-in-the-blanks, questions with short answers, writing of proofs, and drawing of diagrams If you miss this exam for any reason, you will have to appear for a makeup exam on the Thursday of the last week of teaching. That exam will cover all lectures delivered in the semester. It will consist of writing of proofs, drawing of diagrams and answering questions having 0.5-1 page answers.43

Graphs for Computer Network ArchitectureSome local area networks use a star topology, which is a complete bipartite graph K1,n ,as shown in (a). All devices are connected to a central controllerOther local networks are based on a ring topology, where each device is connected to exactly two others using Cn ,as illustrated in (b)Some use a wheel Wn topology, combining the features of both star and ring topologies

Various special graphs also play a role in parallelprocessing where processors need to beinterconnected as one processor may need the output generated by anotherA common way to connect processors in parallel is the mesh network, illustrated here for 16 processorsThe n-cube, Qn, is another way to connect processors in parallel, e.g., Intel Hypercube

44New Graphs from Old: SubgraphsA subgraph of a graph G = (V, E) is a graph (W, F), where W V and F E. A subgraph H of G is a proper subgraph of G if H G

Example: Here we show K5 and one of its subgraphs

45New Graphs from Old: UnionsThe union of two simple graphs G1 = (V1 , E1) andG2 = (V2 , E2) is the simple graph with vertex set V1 V2 and edge set E1 E2. The union of G1 and G2 is denoted by G1 G2

46Representing Graphs and Graph IsomorphismSection 10.347Section SummaryAdjacency ListsAdjacency MatricesIncidence MatricesIsomorphism of Graphs48Representing Graphs: Adjacency List One way to represent a graph without multiple edges is to list all the edges of that graphAnother way to represent a graph with no multiple edges is to use adjacency lists, which specify the vertices that are adjacent to each vertex of the graph

dcbaeAdjacency List Vertex Adjacent Vertices a b, c, e b a c a, d, e d c, e e a, c, d

49Adjacency List for Directed Graphs

Adjacency List for a Directed Graph

Initial vertex Terminal vertices ab, c, d, e bb, d ca, c, e d eb, c, d

de50Representing Graphs: Adjacency MatricesSuppose that G = (V, E) is a simple graph where |V| = n and that the vertices of G are listed arbitrarily asv1 , v2 ,..., vnThe adjacency matrix A of G, AG, with respect to this listing of the vertices, is the n n zero-one matrix with 1 as its (i, j)th entry when vi and vj are adjacent, and 0 as its (i, j)th entry when they are not adjacentIn other words, if its adjacency matrix is A = [aij], then

51Adjacency Matrices: Simple Graphs

The orderingof vertices isa, b, c, d

The orderingof vertices isa, b, c, dNote: The adjacency matrix of a simple graph is symmetric, i.e., aij = aji . Also, since there are no loops, each diagonal entry aii for i = 1, 2, 3, , n, is 0When a graph is sparse, that is, it has few edges relatively to the total number of possible edges, it is much more efficient to represent the graph using an adjacency list than an adjacency matrix

But for a dense graph, which includes a high percentage of possible edges, an adjacency matrix is preferable52Adjacency Matrices: PseudographsAdjacency matrix using the ordering of vertices a, b, c, d

A loop at the vertex vi is represented by a 1 at the (i, j)th position of the matrix

When multiple edges connect the same pair of vertices vi and vj, (or if multiple loops are present at the same vertex), the(i, j)th entry equals the number of edges connecting the pair of vertices53Adjacency Matrices: Directed GraphsThe matrix for a directed graph G = (V, E) has a 1 in its (i, j)th position if there is an edge from vi to vj, where v1, v2, vn is a list of the verticesIn other words, if the graphs adjacency matrix is AG = [aij], then

The adjacency matrix for a directed graph does not have to be symmetric, because there may not be an edge from vi to vj, when there is an edge from vj to viTo represent directed multigraphs, the value of aij is the number of edges connecting vi to vj

54Representing Graphs: Incidence MatricesLet G = (V, E) be an undirected graph

Suppose that v1, v2,..., vn are the vertices and e1, e2,, em are the edges of G

Then the incidence matrix with respect to this ordering of V and E is the n m matrixM = [mij], where

55 e1 e2 e3 e4 e5 e6Incidence Matrices: Simple Graph

56 e1 e2 e3 e4 e5 e6 e7 e8Incidence Matrices: Pseudographs

e8v3

v5Multiple edges are represented in the incidence matrix using columns with identical entries, because these edges are incident with the same pair of vertices

Loops are represented using a column with exactly one entry equal to 1, corresponding to the vertex that is incident with this loop57

Graph IsomorphismTo discuss graph isomorphism, we first need to understand1-to-1 and onto functions581-to-1 and Onto FunctionsA function f is an assignment from set A to set B of exactly 1 element of A to each element of B. We write f(a) = b if b is the unique element of B assigned by the function f to the element a of A.A function f is said to be one-to-one iff f(a) = f(b) implies that a = b for all a and b in the domain.These functions never assign the same value to 2 different domain elements.A function f from A to B is called onto iff for every element b B there is an element a A with f(a) = b.Every element of B is an image of some element of A591-to-1 and Onto Functions

1-to-1,not ontoonto,not 1-to-11-to-1,and ontoneither 1-to-1,nor ontonot afunction60Graph IsomorphismThe simple graphs G = (VG , EG) and H = (VH , EH) are isomorphic if there exists a 1-to-1 and onto functionf from VG to VH with the property that a and b are adjacent in G iff f(a) and f(b) are adjacent in H,a,b VG. Such a function f is called an isomorphism

Isomorphic graphs may look dissimilar, but actually are the same

Two simple graphs that are not isomorphic are called non-isomorphic61Graph Isomorphism: ExampleThe function f with f(u1) = v1, f(u2) = v4, f(u3) = v3, and f(u4) = v2 is a 1-to-1 correspondence between VG and VHTo see that this correspondence preservesadjacency, note that adjacent vertices in G are:u1 and u2u1 and u3u2 and u4u3 and u4and each of the pairsf(u1) = v1 and f(u2) = v4f(u1) = v1 and f(u3) = v3f(u2) = v4 and f(u4) = v2f(u3) = v3 and f(u4) = v2are adjacent in H

62How to Check for Isomorphism?Difficult to determine if 2 simple graphs are isomorphicThere are n! possible 1-to-1 correspondences between the vertex sets of 2 simple graphs with n verticesSometimes it is easier to show that 2 graphs are not isomorphicIn particular, we can show that 2 graphs are not isomorphic if we can find a property only 1 of the 2 graphs has, but that is preserved by isomorphism63Graph InvariantsA property preserved by isomorphism of graphs is called a graph invariant. For example, isomorphic graphs must have:The same number of verticesThe same number of edgesThe degrees of the vertices in the 2 graphs must be the same

If any of these differ in 2 simple graphs, the graphs are non-isomorphicIf they are the same for 2 simple graphs, it does not necessarily mean that the graphs are isomorphic64Checking for Isomorphism: Example 1Show that these graphs are not isomorphic

Both G and H have 5 vertices and 6 edgesHowever, H has a vertex of degree 1, namely, e,whereas G has no vertices of degree 1It follows that G and H are not isomorphic

65Checking for Isomorphism: Example 2Both graphs have 8 vertices and 10 edges

They also both have 4 vertices of degree 2 and 4 of degree 3

Because these invariants all agree, they may be isomorphic

66Checking for Isomorphism: Example 2But, they are not isomorphic

To see this, note that because deg(a) = 2 in G, a must correspond to either t, v, x, or y in H, because these are the vertices of degree 2 in H

However, each of these 4 vertices in H is adjacent to another vertex of degree 2 in H, which is not true for a in G

67Checking for Isomorphism: Example 2Another way to see that G and H are not isomorphic is to note that the subgraphs of G and H made up of vertices of degree 3 and the edges connecting them must be isomorphic if these 2 graphs are isomorphic

Those subgraphs are not isomorphic, therefore G and H are not isomorphic

68CS 103 Discrete Structures Lecture 22Graphs (3)

69Chapter 1 section 1.1 by Dr. Mosaad HassanSecond Midterm Exam1st Lecture in December (same time as the lecture)75 minute durationWill cover all lectures delivered before the exam dateWill consist of MCQs, fill-in-the-blanks, questions with short answers, writing of proofs, and drawing of diagrams If you miss this exam for any reason, you will have to appear for a makeup exam on the Thursday of the last week of teaching. That exam will cover all lectures delivered in the semester. It will consist of writing of proofs, drawing of diagrams and answering questions having 0.5-1 page answers.70

Checking for Isomorphism: Example 3Both G and H have 6 vertices and 7 edges. Both have 4 vertices of degree 2 and 2 vertices of degree 3

It is also easy to see that the subgraphs of G and H consisting of all vertices of degree 2 and the edges connecting them are isomorphic

Because G and H agree with respect to these invariants, it is reasonable to try to find an isomorphism f71

Checking for Isomorphism: Example 3We now will define a function f and then determine whether it is an isomorphismBecause deg(u1) = 2 and u1 is not adjacent to any other vertex of degree 2,the image of u1 must be either v4 or v6, the only vertices of degree 2 in H not adjacent to a vertex of degree 2We arbitrarily set f(u1) = v6If this choice did not lead to isomorphism, we would then try f(u1) = v4

72Checking for Isomorphism: Example 3As u2 is adjacent to u1, the possible images of u2 are v3 and v5We arbitrarily set f(u2) = v3Continuing in this way, using adjacency of vertices and degrees as a guide, we setf(u3) = v4f(u4) = v5f(u5) = v1f(u6) = v2

73Checking for Isomorphism: Example 3We now have a 1-to-1 correspondence between the vertex set of G and the vertex set of H, namely, f(u1) = v6 f(u2) = v3 f(u3) = v4f(u4) = v5f(u5) = v1f(u6) = v2

74Checking for Isomorphism: Example 3

To see whether f preserves edges,we examine AG and AH with therows and columns labeled bythe images of thecorresponding vertices in GBecause AG = AH, it follows that fpreserves edgesAs we have a 1-to-1 correspondencebetween VG and VH, and AG = AH, we conclude thatf is an isomorphism, so G and H are isomorphic75Applications of Graph Isomorphism Chemists use molecular graphs to model chemical compounds. Vertices represent atoms and edges represent chemical bonds. When a new compound is synthesized, a database of molecular graphs is checked to determine whether the graph representing the new compound is isomorphic to the graph of a compound that this already knownElectronic circuits are modeled as graphs in which the vertices represent components and the edges represent connections between them. Graph isomorphism is the basis for the verification that a particular layout of a circuit corresponds to the designs original schematicsdetermining whether a chip from one vendor includes the intellectual property of another vendor76ConnectivitySection 10.477Section SummaryPathsConnectedness in Undirected GraphsConnectedness in Directed Graphs78

In an undirected graph, a path of length n, n Z+, from u to v is a sequence of adjacent edges going from vertex u to vertex vA path is a circuit if u = v and n 0A path traverses the vertices along itA path/circuit is simple if it contains no edge more than once

Simple path: a, d, c, f, e The edges are {a, d}, {d, c}, {c, f }, {f, e}. Path length = 4Circuit: b, c, f, e, b The edges are {b, c}, {c, f }, {f, e}, {e, b}. Path length = 4Not a path: d, e, c, a because {e, c} is not an edgeNot a simple path: a, b, e, d, a, b because {a, b} appears twice. Path length = 5Paths in Undirected Graphs79Paths in Directed GraphsSame as in undirected graphs, but the path must go in the direction of the arrows, e.g. simple path a, b, c dcab80Connected Undirected GraphsAn undirected graph is called connected if there is a path between every pair of distinct vertices of the graph

Connected graph Disconnected graph (no path between a and f, for example)

81Connected Components of a GraphA connected component of a graph G is a connected subgraph of G that is not a proper subgraph of another connected subgraph of GA graph G that is not connected has two or more connected components that are disjoint and have G as their unionThe graph H is the union of3 disjoint subgraphs H1, H2,and H3, none of which areproper subgraphs of a largerconnected subgraph of G.These 3 subgraphs are theconnected components of H

82Strongly/Weakly Connected Directed GraphsStrongly connected directed graphif there is a path from a to b and from b to awhenever a and b are vertices in the graph

Weakly connected directed graphif there is a path between every 2 vertices of thegraph when the direction of edges are disregarded83Strongly & Weakly Connected Graphs: ExampleWeakly connected directed graph(no directed path from a to b)

abdabdccStrongly connected directed graph

84Section 10.1: ExercisesFor Exercises 3, 5, and 7, determine:Whether the graphs shown has directed or undirected edgesWhether it has multiple edges, and whether it has one or more loopsUse your answers to determine the type of graphFor each undirected graph that is not simple, find a set of edges to remove to make it simple

85Section 10.2: ExercisesFind the sum of degreesof each graph in Exercise1, 2, and 3, and verify thatit equals twice the numberof edges in the graph

37. The following graph has 4 vertices. Draw 5 subgraphsof thisgraph thathave 3vertices86Section 10.3: Exercises

87Section 10.4: Exercises1. Does each of these lists of vertices form a path in the this graph? Which paths are simple? Which are circuits? What are the lengths of those that are paths? a, b, e, c, b a, d, a, d, a a, d, b, e, a a, b, e, c, b, d, a

2. Is this graph strongly connected? If not, is it weakly connected?

3. What is the length of the shortest path from e to b?

88