Mod3_Graph_Theory (1)
Mod3_Graph_Theory (1)
Example: How many edges are there in a graph with 10 vertices each of degree
six?
Handshaking Theorem
Example: How many edges are there in a graph with 10 vertices each of degree
six?
Solution: Because the sum of the degrees of the vertices is 6 · 10 = 60, it follows
that 2m = 60 where m is the number of edges. Therefore, m = 30.
Consequent result
Basic Terminologies: Directed Graphs
● When (u, v) is an edge of the graph G with directed edges, u is said to be
adjacent to v and v is said to be adjacent from u. The vertex u is called the
initial vertex of (u, v), and v is called the terminal or end vertex of (u, v).
The initial vertex and terminal vertex of a loop are the same.
● In a graph with directed edges the in-degree of a vertex v, denoted by deg −
(v), is the number of edges with v as their terminal vertex. The out-degree of
v, denoted by deg + (v), is the number of edges with v as their initial vertex.
● Note that a loop at a vertex contributes 1 to both the in-degree and the
out-degree of this vertex.
Basic Terminologies: Directed Graphs
● When (u, v) is an edge of the graph G with directed edges, u is said to be
adjacent to v and v is said to be adjacent from u. The vertex u is called the
initial vertex of (u, v), and v is called the terminal or end vertex of (u, v).
The initial vertex and terminal vertex of a loop are the same.
● In a graph with directed edges the in-degree of a vertex v, denoted by deg −
(v), is the number of edges with v as their terminal vertex. The out-degree of
v, denoted by deg + (v), is the number of edges with v as their initial vertex.
● Note that a loop at a vertex contributes 1 to both the in-degree and the
out-degree of this vertex.
● Theorem:
Basic Terminologies: Directed Graphs
Basic Terminologies: Directed Graphs
Special Simple Graphs
● A complete graph on n vertices, denoted by K_n , is a simple graph that
contains exactly one edge between each pair of distinct vertices. The graphs
K n , for n = 1, 2, 3, 4, 5, 6, are displayed in Figure. A simple graph for which
there is at least one pair of distinct vertex not connected by an edge is called
non-complete.
Special Simple Graphs
● A cycle C_n , n ≥ 3, consists of n vertices v 1 , v 2 , . . . , v n and edges {v 1 , v
2 }, {v 2 , v 3 }, . . . , {v n−1 , v n }, and {v n , v 1 }. The cycles C 3 , C 4 , C 5 ,
and C 6 are displayed in Figure.
Special Simple Graphs
● A cycle C_n , n ≥ 3, consists of n vertices v 1 , v 2 , . . . , v n and edges {v 1 , v
2 }, {v 2 , v 3 }, . . . , {v n−1 , v n }, and {v n , v 1 }. The cycles C 3 , C 4 , C 5 ,
and C 6 are displayed in Figure.
Graph G is bipartite because its vertex set is the union of two disjoint sets, {a, b, d}
and {c, e, f, g}, and each edge connects a vertex in one of these subsets to a
vertex in the other subset. Graph H is not bipartite because its vertex set cannot
be partitioned into two subsets so that edges do not connect two vertices from the
same subset.
Bipartite graph property
Bipartite graph property
Complete Bipartite Graphs
● A complete bipartite graph K m,n is a graph that has its vertex set partitioned
into two subsets of m and n vertices, respectively with an edge between two
vertices if and only if one vertex is in the first subset and the other vertex is in
the second subset.
● The complete bipartite graphs K 2,3 , K 3,3 , K 3,5 , and K 2,6 are displayed
in Figure.
Bipartite Graphs and Matchings
● A matching M in a simple graph G = (V , E) is a subset of the set E of edges
of the graph such that no two edges are incident with the same vertex.
● In other words, a matching is a subset of edges such that if {s, t} and {u, v}
are distinct edges of the matching, then s, t, u, and v are distinct.
● A maximum matching is a matching of maximum size (maximum number of
edges).
● Suppose that there are m employees in a group and n different jobs that need
to be done, where m ≥ n. Each employee is trained to do one or more of
these n jobs. We would like to assign an employee to each job.
Bipartite Graphs and Matchings
● Suppose that there are m employees in a group and n different jobs that need
to be done, where m ≥ n. Each employee is trained to do one or more of
these n jobs. We would like to assign an employee to each job.
New Graphs from Old
● A subgraph of a graph G = (V , E) is a graph H = (W, F ), where W ⊆ V and F
⊆ E. A subgraph H of G is a proper subgraph of G if H ≠ G.
● Let G = (V , E) be a simple graph. The subgraph induced by a subset W of
the vertex set V is the graph (W, F ), where the edge set F contains an edge
in E if and only if both endpoints of this edge are in W .
New Graphs from Old
● A subgraph of a graph G = (V , E) is a graph H = (W, F ), where W ⊆ V and F
⊆ E. A subgraph H of G is a proper subgraph of G if H ≠ G.
● Let G = (V , E) be a simple graph. The subgraph induced by a subset W of
the vertex set V is the graph (W, F ), where the edge set F contains an edge
in E if and only if both endpoints of this edge are in W .
● Example: The graph G shown in Figure is a subgraph of K_5 .
New Graphs from Old
● A subgraph of a graph G = (V , E) is a graph H = (W, F ), where W ⊆ V and F
⊆ E. A subgraph H of G is a proper subgraph of G if H ≠ G.
● Let G = (V , E) be a simple graph. The subgraph induced by a subset W of
the vertex set V is the graph (W, F ), where the edge set F contains an edge
in E if and only if both endpoints of this edge are in W .
● Example: The graph G shown in Figure is a subgraph of K_5 . If we add the
edge connecting c and e to G, we obtain the subgraph induced by W = {a, b,
c, e}.
Removing or adding edges of a graph
● Given a graph G = (V , E) and an edge e ∈ E, we can produce a subgraph of
G by removing the edge e. The resulting subgraph, denoted by G − e, has the
same vertex set V as G. Its edge set is E − e. Hence, G − e = (V , E − {e}).
● Similarly, if E’ is a subset of E, we can produce a subgraph of G by removing
the edges in E’ from the graph. The resulting subgraph has the same vertex
set V as G. Its edge set is E − E’ .
● We can also add an edge e to a graph to produce a new larger graph when
this edge connects two vertices already in G. We denote by G + e the new
graph produced by adding a new edge e, connecting two previously
nonincident vertices, to the graph G. Hence, G + e = (V , E ∪ {e}).
● The vertex set of G + e is the same as the vertex set of G and the edge set is
the union of the edge set of G and the set {e}.
Edge contractions
● Sometimes when we remove an edge from a graph, we do not want to retain
the endpoints of this edge as separate vertices in the resulting subgraph.
● In such a case we perform an edge contraction which removes an edge e with
endpoints u and v and merges u and w into a new single vertex w, and for
each edge with u or v as an endpoint replaces the edge with one with w as
endpoint in place of u or v and with the same second endpoint.
● Hence, the contraction of the edge e with endpoints u and v in the graph G =
(V , E) produces a new graph G = (V , E ) (which is not a subgraph of G),
where V = V − {u, v} ∪ {w} and E contains the edges in E which do not have
either u or v as endpoints and an edge connecting w to every neighbor of
either u or v in V .
Some more operations
● When we remove a vertex v and all edges incident to it from G = (V , E), we
produce a subgraph, denoted by G − v. Observe that G − v = (V − v, E’),
where E’ is the set of edges of G not incident to v.
● Similarly, if V’ is a subset of V , then the graph G − V’ is the subgraph (V − V’,
E’), where E’ is the set of edges of G not incident to a vertex in V’ .
● The union of two simple graphs G 1 = (V 1 , E 1 ) and G 2 = (V 2 , E 2 ) is the
simple graph with vertex set V 1 ∪ V 2 and edge set E 1 ∪ E 2 . The union of
G 1 and G 2 is denoted by G 1 ∪ G 2 .
Graph Representation
1. Edge list: List all edges in graph.
2. Adjacency lists: which specify the vertices that are adjacent to each vertex of
the graph.
Graph Representation
1. Edge list: List all edges in graph.
2. Adjacency lists: which specify the vertices that are adjacent to each vertex of
the graph.
Graph Representation
1. Adjacency Matrix: Suppose that G = (V , E) is a simple graph where |V | = n.
Suppose that the vertices of G are listed arbitrarily as v 1 , v 2 , . . . , v n . The
adjacency matrix A (or A G ) of G, with respect to this listing of the vertices, is the n
x n zero–one matrix with 1 as its (i, j )th entry when v i and v j are adjacent, and 0
as its (i, j )th entry when they are not adjacent. In other words, if its adjacency
matrix is A = [a ij ], then
Graph Representation
1. Adjacency Matrix: Suppose that G = (V , E) is a simple graph where |V | = n.
Suppose that the vertices of G are listed arbitrarily as v 1 , v 2 , . . . , v n . The
adjacency matrix A (or A G ) of G, with respect to this listing of the vertices, is the n
x n zero–one matrix with 1 as its (i, j )th entry when v i and v j are adjacent, and 0
as its (i, j )th entry when they are not adjacent. In other words, if its adjacency
matrix is A = [a ij ], then
Graph Representation
1. Adjacency Matrix: Suppose that G = (V , E) is a simple graph where |V | = n.
Suppose that the vertices of G are listed arbitrarily as v 1 , v 2 , . . . , v n . The
adjacency matrix A (or A G ) of G, with respect to this listing of the vertices, is the n
x n zero–one matrix with 1 as its (i, j )th entry when v i and v j are adjacent, and 0
as its (i, j )th entry when they are not adjacent. In other words, if its adjacency
matrix is A = [a ij ], then
Graph Representation
1. Adjacency Matrix: Suppose that G = (V , E) is a simple graph where |V | = n.
Suppose that the vertices of G are listed arbitrarily as v 1 , v 2 , . . . , v n . The
adjacency matrix A (or A G ) of G, with respect to this listing of the vertices, is the n
x n zero–one matrix with 1 as its (i, j )th entry when v i and v j are adjacent, and 0
as its (i, j )th entry when they are not adjacent. In other words, if its adjacency
matrix is A = [a ij ], then
Graph Representation
1. Adjacency Matrix: Suppose that G = (V , E) is a simple graph where |V | = n.
Suppose that the vertices of G are listed arbitrarily as v 1 , v 2 , . . . , v n . The
adjacency matrix A (or A G ) of G, with respect to this listing of the vertices, is the n
x n zero–one matrix with 1 as its (i, j )th entry when v i and v j are adjacent, and 0
as its (i, j )th entry when they are not adjacent. In other words, if its adjacency
matrix is A = [a ij ], then
Graph Representation: More about adjacency matrix
● The adjacency matrix of a simple graph is symmetric, that is, a ij = a j i , because
both of these entries are 1 when v i and v j are adjacent, and both are 0 otherwise.
Furthermore, as a simple graph has no loops, each entry a ii , i = 1, 2, . . , n, is 0.
● Adjacency matrices can also be used to represent undirected graphs with loops
and with multiple edges.
● A loop at the vertex v i is represented by a 1 at the (i, i)th position of the adjacency
matrix.
● When multiple edges connecting the same pair of vertices v i and v j , or multiple
loops at the same vertex, are present, the adjacency matrix is no longer a
zero–one matrix, because the (i, j )th entry of this matrix equals the number of
edges that are associated to {v i , v j }.
● All undirected graphs, including multigraphs and pseudographs, have symmetric
adjacency matrices.
Graph Representation
Q) Use an adjacency matrix to represent the pseudograph shown in Figure.
Graph Representation
Q) Use an adjacency matrix to represent the pseudograph shown in Figure.
Graph Representation: Adjacency matrix for Directed
Graphs
● The matrix for a directed graph G = (V , E) has a 1 in its (i, j )th position if
there is an edge from v i to v j , where v 1 , v 2 , . . . , v n is an arbitrary listing
of the vertices of the directed graph.
● In other words, if A = [a ij ] is the adjacency matrix for the directed graph with
respect to this listing of the vertices, then
Graph Representation: Adjacency matrix for Directed
Graphs
● The matrix for a directed graph G = (V , E) has a 1 in its (i, j )th position if
there is an edge from v i to v j , where v 1 , v 2 , . . . , v n is an arbitrary listing
of the vertices of the directed graph.
● In other words, if A = [a ij ] is the adjacency matrix for the directed graph with
respect to this listing of the vertices, then
Graph Representation: Adjacency matrix for Directed
Graphs
● The matrix for a directed graph G = (V , E) has a 1 in its (i, j )th position if
there is an edge from v i to v j , where v 1 , v 2 , . . . , v n is an arbitrary listing
of the vertices of the directed graph.
● In other words, if A = [a ij ] is the adjacency matrix for the directed graph with
respect to this listing of the vertices, then
● The adjacency matrix for a directed graph does not have to be symmetric,
because there may not be an edge from v j to v i when there is an edge from
v i to v j .
● In the adjacency matrix for a directed multigraph, a ij equals the number of
edges that are associated to (v i , v j ).
Adjacency list versus Adjacency matrix: Sparse case
Adjacency list versus Adjacency matrix: Sparse case
● When a simple graph contains relatively few edges, that is, when it is sparse,
it is usually preferable to use adjacency lists rather than an adjacency matrix
to represent the graph. For example, if each vertex has degree not exceeding
c, where c is a constant much smaller than n, then each adjacency list
contains c or fewer vertices. Hence, there are no more than cn items in all
these adjacency lists. On the other hand, the adjacency matrix for the graph
has n^2 entries.
● Note, however, that the adjacency matrix of a sparse graph is a sparse matrix,
that is, a matrix with few nonzero entries, and there are special techniques for
representing, and computing with, sparse matrices.
Adjacency list versus Adjacency matrix: Dense case
Adjacency list versus Adjacency matrix: Dense case
● For a dense graph, that is, suppose that it contains many edges, such as a
graph that contains more than half of all possible edges. In this case, using an
adjacency matrix to represent the graph is usually preferable over using
adjacency lists.
● To see why, we compare the complexity of determining whether the possible
edge {v i , v j } is present. Using an adjacency matrix, we can determine
whether this edge is present by examining the (i, j )th entry in the matrix. This
entry is 1 if the graph contains this edge and is 0 otherwise. Consequently, we
need make only one comparison, to determine whether this edge is present.
● On the other hand, when we use adjacency lists to represent the graph, we
need to search the list of vertices adjacent to either v i or v j to determine
whether this edge is present. This can require (|V |) comparisons when many
edges are present.
Graph Representation: Incidence matrix
Another common way to represent graphs is to use incidence matrices. Let G =
(V , E) be an undirected graph. Suppose that v 1 , v 2 , . . . , v n are the vertices
and e 1 , e 2 , . . . , e m are the edges of G. Then the incidence matrix with
respect to this ordering of V and E is the n × m matrix M = [m ij ], where
Graph Representation: Incidence matrix
Another common way to represent graphs is to use incidence matrices. Let G =
(V , E) be an undirected graph. Suppose that v 1 , v 2 , . . . , v n are the vertices
and e 1 , e 2 , . . . , e m are the edges of G. Then the incidence matrix with
respect to this ordering of V and E is the n × m matrix M = [m ij ], where
Examples:
Graph Representation: Incidence matrix
Another common way to represent graphs is to use incidence matrices. Let G =
(V , E) be an undirected graph. Suppose that v 1 , v 2 , . . . , v n are the vertices
and e 1 , e 2 , . . . , e m are the edges of G. Then the incidence matrix with
respect to this ordering of V and E is the n × m matrix M = [m ij ], where
Examples:
Graph Representation: Incidence matrix
Another common way to represent graphs is to use incidence matrices. Let G =
(V , E) be an undirected graph. Suppose that v 1 , v 2 , . . . , v n are the vertices
and e 1 , e 2 , . . . , e m are the edges of G. Then the incidence matrix with
respect to this ordering of V and E is the n × m matrix M = [m ij ], where
Examples:
Isomorphism of Graphs
● The simple graphs G 1 = (V 1 , E 1 ) and G 2 = (V 2 , E 2 ) are isomorphic if
there exists a one-to-one and onto function f from V 1 to V 2 with the property
that a and b are adjacent in G 1 if and only if f (a) and f (b) are adjacent in G 2,
for all a and b in V 1 . Such a function f is called an isomorphism.
● Two simple graphs that are not isomorphic are called nonisomorphic.
● In other words, when two simple graphs are isomorphic, there is a one-to-one
correspondence between vertices of the two graphs that preserves the
adjacency relationship.
● Isomorphism of simple graphs is an equivalence relation.
Example: Isomorphism of Graphs
Show that the graphs G = (V , E) and H = (W, F ),
displayed in Figure are isomorphic.
Soln:
Example: Isomorphism of Graphs
Show that the graphs G = (V , E) and H = (W, F ), displayed
in Figure are isomorphic.
Soln: Both G and H have five vertices and six edges. However, H has a vertex of
degree one, namely, e, whereas G has no vertices of degree one. It follows that G
and H are not isomorphic.
Isomorphism using Adjacency matrix
● To show that a function f from the vertex set of a graph G to the vertex set of a
graph H is an isomorphism, we need to show that f preserves the presence
and absence of edges.
● One helpful way to do this is to use adjacency matrices. In particular, to show
that f is an isomorphism, we can show that the adjacency matrix of G is the
same as the adjacency matrix of H , when rows and columns are labeled to
correspond to the images under f of the vertices in G that are the labels of
these rows and columns in the adjacency matrix of G.
Example: Isomorphism using Adjacency matrix
Determine whether the graphs G and H displayed in Figure are isomorphic.
Solution: Both G and H have six vertices and seven edges. Both have four
vertices of degree two and two vertices of degree three. It is also easy to see that
the subgraphs of G and H consisting of all vertices of degree two and the edges
connecting them are isomorphic (as the reader should verify). Because G and H
agree with respect to these invariants, it is reasonable to try to find an
isomorphism f .
Example: Isomorphism using Adjacency matrix
We now will define a function f and then determine whether it is an isomorphism.
Because deg(u 1 ) = 2 and because u 1 is not adjacent to any other vertex of
degree two, the image of u 1 must be either v 4 or v 6 , the only vertices of degree
two in H not adjacent to a vertex of degree two. We arbitrarily set f (u 1 ) = v 6 .
Because u 2 is adjacent to u 1 , the possible images of u 2 are v 3
The graph H is not strongly connected. There is no directed path from a to b in this
graph. However, H is weakly connected, because there is a path between any two
vertices in the underlying undirected
graph of H.
Strong Components of Directed graph
● The subgraphs of a directed graph G that are strongly connected but not
contained in larger strongly connected subgraphs, that is, the maximal
strongly connected subgraphs, are called the strongly connected components
or strong components of G.
● Example: The graph H in Figure has three strongly connected components,
consisting of the vertex a; the vertex e; and the subgraph consisting of the
vertices b, c, and d and edges (b, c), (c, d), and (d, b).
Application: Web Graph
● The Web graph represents Web pages with vertices and links with directed
edges. A snapshot of the Web in 1999 produced a Web graph with over 200
million vertices and over 1.5 billion edges.
● The underlying undirected graph of this Web graph is not connected, but it
has a connected component that includes approximately 90% of the vertices
in the graph.
● The subgraph of the original directed graph corresponding to this connected
component of the underlying undirected graph (that is, with the same vertices
and all directed edges connecting vertices in this graph) has one very large
strongly connected component and many small ones.
● The former is called the giant strongly connected component (GSCC) of the
directed graph. A Web page in this component can be reached following links
starting at any other page in this component. The GSCC in the Web graph
produced by this study was found to have over 53 million vertices.
Paths and Isomorphism
● There are several ways that paths and circuits can help determine whether
two graphs are isomorphic like the existence of a simple circuit of a particular
length is a useful invariant that can be used to show that two graphs are not
isomorphic.
● Example: Determine whether the graphs G and H shown in Figure are
isomorphic.
Paths and Isomorphism
● There are several ways that paths and circuits can help determine whether
two graphs are isomorphic like the existence of a simple circuit of a particular
length is a useful invariant that can be used to show that two graphs are not
isomorphic.
● Example: Determine whether the graphs G and H shown in Figure are
isomorphic.
● Solution: Both G and H have six vertices and
eight edges, and the degrees of vertices also agree.
However, H has a simple circuit of length three, namely, v
1 , v 2 , v 6 , v 1 , whereas G has no simple circuit of
length three. Because the existence of a simple
circuit of length three is an isomorphic invariant,
G and H are not isomorphic.
Counting Paths Between Vertices
● Let G be a graph with adjacency matrix A with respect to the ordering v 1 , v 2
, . . . , v n of the vertices of the graph (with directed or undirected edges, with
multiple edges and loops allowed).
● The number of different paths of length r from v i to v j , where r is a positive
integer, equals the (i, j )th entry of A^r .
Counting Paths Between Vertices
Example: How many paths of length four are there from a to d in the simple graph
G in Figure?
Counting Paths Between Vertices
Example: How many paths of length four are there from a to d in the simple graph
G in Figure?
Königsberg problem
● The town of Königsberg, Prussia was divided into four sections by the branches
of the Pregel River. These four sections included the two regions on the banks
of the Pregel, Kneiphof Island, and the region between the two branches of the
Pregel. In the eighteenth century seven bridges connected these regions.
● The townspeople took long walks through town on Sundays. They wondered
whether it was possible to start at some location in the town, travel across all the
bridges once without crossing any bridge twice, and return to the starting point.
Solution: Königsberg problem - Euler circuit
● The Swiss mathematician Leonhard Euler solved this problem. His solution,
published in 1736, may be the first use of graph theory.
● Euler studied this problem using the multigraph obtained when the four
regions are represented by vertices and the bridges by edges.
● The problem of traveling across every bridge without crossing any bridge
more than once can be rephrased in terms of this model. The question
becomes: Is there a simple circuit in this multigraph that contains every edge?
● An Euler circuit in a graph G is a simple circuit containing every edge of G.
An Euler path in G is a simple path containing every edge of G.
Example: Euler circuit or path
Which of the undirected graphs in Figure have an Euler circuit? Of those that do
not, which have an Euler path?
Example: Euler circuit or path
Which of the undirected graphs in Figure have an Euler circuit? Of those that do
not, which have an Euler path?
It is a greedy algorithm. The main logic of this algorithm is based on the following
formula- dist[r]=min(dist[r], dist[q]+cost[q][r]).
This formula states that distance vertex r, which is adjacent to vertex q, will be
updated if and only if the value of dist[q]+cost[q][r] is less than dist[r].
Dijkstra's Algorithm
Step 1; Set dist[s]=0, S=ϕ // s is the source vertex and S is a 1-D array having all
the visited vertices
Step 3: find q not in S such that dist[q] is minimum // vertex q should not be visited
Step 5: update dist[r] for all r adjacent to q such that r is not in S //vertex r should
not be visited dist[r]=min(dist[r], dist[q]+cost[q][r]) //Greedy and Dynamic approach
Step 6: Repeat Steps 3 to 5 until all the nodes are in S // repeat till all the vertices
have been visited
Step 7: Print array dist having shortest path from the source vertex u to all other
vertices
Example:
Example:
Example:
Example:
Example:
Recap
More about shortest path problem
More algorithms:
Note: All images unless cited are from one of the above sources