2
A Graph, G = (V, E), is a data structure which consists of a nonempty set of vertices (or nodes), V, and a set of edges, E, that connect (some of) them.
The set of vertices V of a graph G may be infinite. A graph with infinite vertex set is called infinite graph. A graph with finite vertex set is called finite graph. For example,
V = {1, 2, 3, 4, 5} E = { (1,2), (1,3), (1,4), (2,3), (3,5), (4,5) }
1
2
3
45
Vertex(Node)Edge
What is a Graph?
3
Computer Networks
Electrical Circuits
Road Map
Computer
Resistor/Inductor/…
City
Application of Graphs
4
An Undirected Graph is a graph where the edges have no directions.
The edges in an undirected graph are called Undirected Edges
For example, G = (V, E), where V = {1, 2, 3, 4, 5} and E = {(1,2), (1,3), (1,4), (2,3), (3,5),
(4,5)} A Directed Graph or Digraph is a graph where each edge has a
direction. The edges in a digraph
are called Arcs or Directed Edges For example, G = (V, E), where V = {1, 2, 3, 4, 5, 6} and
E = {(1,4), (2,1), (2,3), (3,2), (4,3), (4,5), (4,6), (5,3), (6,1), (6,5)}
The edge (1, 4) = 1→4 where 1 is the tail and 4 is the head
1
2
3
45
Graph Categorization
1
4
3
6
52
5
1
2
3
45
40
7060 20
50
10
A graph with both directed and undirected edges is called a mixed graph.
Adjacent vertices: If (u, v) is an edge of the graph, then the nodes u and v are adjacent.
An edge (u, v) is incident to vertices u and v. For example, Vertices 1 and 2 are adjacent,
but vertices 2 and 5 are not adjacent A Weighted Graph is a graph where
all the edges are assigned weights. If the same pair of vertices have more
than one edges (multiple edges), that graph is called a Multigraph
1
2 3
Graph Categorization
12
3
45
6
Loop or self edges: An edge (u, u) is called a self edge or a loop.
( 1,1 ) and ( 4,4 ) are self edges Graphs that may include loops,
and possibly multiple edges connecting same pair of nodes, are called pseudographs.
In simple graphs (or graphs) loops and multiple edges are not permitted
The complete graph on n vertices, Kn, is the simple graph that contains exactly one edge between each pair of distinct nodes.
12
3
45
1 21
2 3
1 2
3 4
1
2 3
4 5
Graph Categorization
7
The cycle Cn, n≥3, consists of n vertices v1, v2, ….., vn, and edges (v1, v2), (v2, v3), … , (vn-1, vn), and (vn, v1).
That is, Cn consists of n vertices and n edges.
A simple graph G is called bipartite if its vertex set can be partitioned into two disjoint sets V1 and V2 such that every edge in the graph connects a vertex in V1 and a vertex in V2. When this condition holds, we call the pair (V1, V2) a bipartition of the vertex set V of G.
1
2 3
1 2
3 4
1
2 3
4 5
Graph Categorization
8
Example 1:- C6 is bipartite, because
its vertex set can be partitionedinto two disjoint sets V1={v1,v3,v5}
and V2={v2,v4,v6}, and every edge
of C6 connects a vertex in V1 and a vertex in V2.
Example 2:- K3 is not bipartite, because if we divide the vertex set into two disjoint sets, one set must contain two vertices. If the graph were bipartite, these two vertices could not be connected by an edge, but in K3 each vertex is connected to every other vertex by an edge.
Theorem:- A simple graph is bipartite if and only if it is possible to assigns one of two different colors to each vertex of the graph so that no two adjacent vertices are assigned the same color.
Graph Categorization
V1
v1 .v3 .v5 .
V2
. v2
. v4
. v6
9
A Subgraph of graph G=(V,E) is a graph H=(U,F) such that U⊆ V and F ⊆ E. A subgraph H is a proper subgraph of G if H≠G.
The union of two simple graphs G1=(V1, E1) and G2=(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. For example,
12
3
45
G=(V,E)
1 3
2
H=(U,F)
Graph Categorization
12
3
4
G1
2
3
45
G2
12
3
45
G1 U G2
10
Degree of a Vertex : In an undirected graph, the no. of edges incident to the vertex. Degree of vertex v is denoted by deg(v).
For example, deg(1)=3, deg(2)=2, deg(3)=2, deg(4)=1 and deg(5)=0.
A vertex of degree zero is called isolated vertex. A vertex of degree one is called pendant vertex. For example, vertex 4 is pendant and vertex 5 is isolated. The Handshaking Theorem: - Let G=(V, E) be an
undirected graph. Then 2|E| = ∑deg(v), ∀v ∈ V Example 3:- 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 6x10=60, it follows that 2|E|=60. Therefore, |E|=30.
Graph Terminology
12
3
45
11
In diagram, there are two types of degree of a vertex - in-degree and out-degree
In-degree: In-degree of a vertex v, denoted by deg-(v), is the no. of edges entering the vertex in a digraph
Out-Degree: Out-degree of a vertex v, denoted by deg+(v), is the no. of edges leaving the vertex
For example, deg-(1)=3 and deg+(1)=1.deg-(2)=1 and deg+(2)=1.
Theorem:- For a digraph G=(V, E), ∑deg-(v)+∑deg+(v)=|E|, ∀v∈V
12
3
45
Graph Terminology
12
Adjacency Matrix:- The Adjacency Matrix A=(ai,j) of a graph G=(V,E) with n nodes is an nXn zero-one matrix. Element of A can be defined as follows:
aij = 1, if (vi, vj) Є E,
= 0, otherwise Example 4: Find the adjacency matrices of the following
graphs.
1
2
3
45
12345
101110
210100
311001
410001
500110
1
2
3
123
1010
2001
3110
Representation of Graphs
13
Adjacency Matrix of a Weighted Graph:- The weight of the edge can be shown in the matrix when the vertices are adjacent A nil value (0 or ∞) depending on the problem is used
when they are not adjacent Example 5: Adjacency matrix
to find the minimum distance between nodes
Example 6: Draw graph with the adjacency matrix
123
1∞4∞
2∞∞5
395∞
1
2
39
4 5
Representation of Graphs
123
1∞19
21∞∞
3∞8∞
1
2
39
1 8
14
Adjacency List:- An Adjacency list is an array of lists, each list showing the vertices a given vertex is adjacent to….
Example 7: Find the adjacency list of the following graph.
Adjacency List of a Weighted Graph:- The weight is included in the list
Example 8: Find the adjacency list of the following graph.
1
2
3
45
12345
2
1 31 51 5
1
2
39
4 5
1
2
3
2 4
3 5
1 9 2 5
Representation of Graphs
15
Incidence Matrix:- Let G=(V,E) be a graph with v1, v2, …, vn are vertices and e1, e2, …., vm are edges. Then the incidence matrix with respect to this ordering of V and E is the nxm matrix M=[mij], where
mij = 1, when edge ej is incidence on vi,
= 0, otherwise Example 9: Find the incidence matrices of the following
graphs. e1e2e3e4e5e6
v1111000
v2100100
v3010110
v4001001
v5000011
e1e2e3
v1100
v2011
v3000
Representation of Graphs
v1
v2
v3
v4
v5
e2
e5
e1e4
e6
e3
v1
v2
v3
e1
e2 e3
16
Def:- The simple graphs G1=(V1, E1) and G2=(V2, E2) are isomorphic if there is one-to-one and onto function f from V1 to V2 with the property that a and b are adjacent in G1 if and only if f(a) and f(b) are adjacent in G2, for all a, b in V1. Such a function f is called an isomorphism.
Example 10: Show that the graphs G=(V, E) and H=(U, F) are isomorphic.
Proof:- The function f with f(v1)=u1, f(v2)=u4, f(v3)=u3, and f(v4)=u2 is a one-to-one correspondence between V and U. Next, to verify that this function preserves adjacency also. Adjacency vertices in G are v1 and v2, v1 and v3, v2 and v4, and v3 and v4. Also, the pairs f(v1)=u1 and f(v2)=u4, f(v1)=u1 and f(v3)=u3, f(v2)=u4 and f(v4)=u2, f(v3)=u3, and f(v4)=u2 are adjacent in H.
Isomorphism of Graphs
v1 v2
v3 v4
u1 u2
u3 u4
GH
17
A a path a sequence of edges that begins at a vertex of a graph and travels from vertex to vertex along the edges of the graph.
There can be more than one path between two vertices. Vertex u is reachable from v if there is a path from u to v. Paths from 5 to 1 are 5, 3, 1 and 5, 3, 2, 1 A Simple Path is a path, where all the
vertices are distinct. For example, 1,4,5,3 is a simple path, but 1,4,5,4 is not Length of a path is the no. of edges considered in the path. Length of the path 1,4,5,3 is 3 A circuit is a path whose first and last vertices are the same. For example, the path 3,2,1,4,5,3 is a circuit
1
2
3
45
Connectivity
18
A Cycle is a circuit where all the vertices are distinct except for the first (and the last) vertex.
For example, 1,4,5,3,1 is a cycle, but 1,4,5,4,1 is not a cycle.
Hamiltonian Cycle: A Cycle that contains all the vertices of the graph.
1,4,5,3,2,1 is a Hamiltonian Cycle. A graph is said to be connected if there is at least one
path from every vertex to every other vertex in the graph. 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.
For example, G has an Eulercircuit, but H does not have
1
2
3
45
Euler Paths
1 2
3 4
5
G
1 2
3 4
5
H
19
Tree: A connected undirected graph that contains no cycles
Forest: A graph that does not contain a cycle
The Spanning Tree of a Graph G is a subgraph of G that is a tree and contains all the vertices of G.
12
3
45
Tree
1
2
3
45
Forest
Spanning Tree
1
2
3
45
Graph
1
2
3
45
Spanning Tree
1 3
45
Tree