Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

CPE133 – Discrete Structures

Lecture Notes

Lecture 7
Graphs and Search Techniques
Graphs
Definition: A graph consists of a non-empty set of vertices or nodes V
and a set of edges E
Graph is denoted as G = (V, E)
Example: Let us consider, a Graph is G = (V, E)
where V = {a, b, c, d}
E = { ab,ac,bc,cd}
Nodes - vertices or end points in graph (a,b,c,d)
Edge – line joining two nodes or endpoints in graph (ab, ac,bc,cd)
Importance of graph theory
- We can determine whether two computers are connected by a
communications link using graph models of computer networks.
- Graphs with weights assigned to their edges can be used to solve
problems such as finding the shortest path between two cities in a
transportation network.
Loop- an edge that joins a vertex to itself
(a join to a)
multiple edges - if there is more than one edge
joining two vertices ( CD)

Directed and Undirected Graph


- A graph is directed graph if edge set is made of ordered vertex pair
In Fig below, Edges are ab (not ba), bc (not cb), ca (not ac)
- A graph is undirected if edge set is made of unordered vertex pair
In Fig below, Edges are ab, bc, ac (here edges ab & ba are same, ac &
ca are same, bc & cb are same)
Example: determine whether the graph shown has directed or
undirected edges, whether it has multiple edges, and whether it has
one or more loops
G – undirected graph
One loop (b), No multiple edges

H - directed graph
No loop, two multiple edges (ab, bd)
Adjacent nodes and degree of undirected graph
Two vertices u and v in an undirected graph G are called adjacent (or
neighbors) in G if u and v are endpoints of an edge e.
Such an edge e is called incident with vertices u and v and
e is said to connect u and v
a, b are adjacent nodes (connected by edge ab)
a, c are adjacent nodes

Degree of Vertex
The degree of a vertex V of a undirected graph G (denoted by deg (V)) is the
number of edges incident with vertex V.
In above graph; deg(a)=2, deg(b)=2, deg(c)=3, deg(d)=1, deg(e)=0
pendant vertex – vertex with degree one; (d)
isolated vertex - vertex of degree zero; (e)
An isolated vertex is not adjacent to any vertex
A vertex having loop has additional degree 2 for the loop
Degree of Graph
The degree of a graph is the largest vertex degree of that graph.
For above graph the degree of the graph is 3.
The Handshaking Lemma:
In a graph, sum of all degrees of all vertices is equal to twice
the number of edges

Example: How many edges are there in a graph with 10


vertices each of degree six?
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
Example: (a) List number of vertices (nodes) and edges of graph G
(b) What are degrees of vertex (c) what are neighborhoods (adjacent)
of vertices in graphs G (d) what is degree of graph
(e) Show sum of degs=2edges
(f) list pendent vertex
(g) list isolated vertex

Vertices = {a,b,c,d,e,f,g,h} = 8
Edges = {aa,ab, af, bc,be,bf, cd,cd,ce,cf,ef,eg} = 12
deg(a)=deg(b)=deg(f)= deg(e)= 4,deg(c)=5,deg(d)=2,deg(g)=1,deg(h)=0
degree of graph = MAX deg = 5
The neighborhoods (adj)of vertices are N(a) = {a,b, f },
N(b) = {a, c, e, f }, N(c) = {b, d, e, f }, N(d) = {c}, N(e) = {b, c, f },
N(f ) = {a, b, c, e}, N(g) ={e} N(h) = ∅
sum of degs=4x4+5+2+1=24 2edges =2x12=24
pendent vertex =vertex with deg(1) =g
isolated vertex =vertex with deg(0) =h
Adjacent nodes and degree of directed graph
When (u, v) is an edge of 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 initial vertex of (u, v),
v is called terminal or end vertex of (u, v).
The initial vertex (w) and terminal vertex (w) of a loop
are same
In a graph with directed edges
in-degree of a vertex v, deg−(v), is number of edges with v as their
terminal vertex. [deg−(u)= 1,deg−(v)=1, deg−(w)=2 ]
out-degree of v, deg+(v), is number of edges with v as their initial
vertex. [deg+(u)= 1,deg+(v)=1, deg+(w)=2 ]
(Note that a loop at a vertex contributes 1 to both the in-degree
and the out-degree of this vertex)
sum of in-degrees = sum of out-degrees = number of edges
(above graph, 4)
Example: (a) List the number of vertices and edges of graph G
(b) Find in-degree and out-degree of each vertex in the graph G
with directed edges
(c) Show sum of in-degree =
sum of out-degree = edges

Vertices = {a,b,c,d,e,f} = 6
Edges = {aa, ab, ac, ae, bd, cb,cc, dc,de, ea,ed,ee} = 12
in-degrees , deg−(a)= 2, deg−(b)= 2, deg−(c)= 3, deg−(d)= 2,
deg−(e)= 3, deg−(f )= 0
out-degrees, deg+(a)= 4, deg+(b)= 1, deg+(c)= 2, deg+(d)= 2,
deg+(e)= 3, deg+(f )= 0
Sum of in-degree = sum of out-degree =12 = edges
Null Graph
A null graph has no edges. null graph of n vertices is denoted by Nn

Connected and Disconnected Graph


graph is connected if any two vertices of graph are connected by path
graph is disconnected if at least two vertices of graph are not
connected by path.
Regular Graph
A graph is regular if all vertices of graph
have the same degree.
In a regular graph G of degree r, the
degree of each vertex of G is r.

Complete Graph
A graph is called complete graph if every
two vertices pair are joined by exactly one
edge. The complete graph with n vertices
is denoted by Kn

Cycle Graph
If a graph consists of a single cycle, it is
called cycle graph. The cycle graph with n
vertices is denoted by Cn
Bipartite Graph
If the vertex-set of a graph G can be split into two disjoint sets, 𝑉1
and 𝑉2, in such a way that each edge in the graph joins a vertex in 𝑉1
to a vertex in 𝑉2, and there are no edges in G that connect two
vertices in 𝑉1 or two vertices in 𝑉2, then the graph G is called a
bipartite graph

Example: Is graphs C, bipartite or not

C is bipartite, because its vertex set can be partitioned into the two
sets V1 = {v1, v3, v5} and V2 = {v2, v4, v6}, and every edge of C
connects a vertex in V1 and a vertex in V2
GRAPH UNION
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

Example: Find union of graphs G1 and G2 shown in Fig

The vertex set of the union G1 ∪ G2 is union of two vertex sets,


namely, {a, b, c, d, e, f }. The edge set of the union is the union of
the two edge sets. The union is displayed in Fig
Representation of Graphs
There are mainly two ways to represent a graph:
Adjacency List
Adjacency Matrix
Incidence matrices
Adjacency List
adjacency lists specify vertices that are adjacent to each vertex of
the graph.
Example: Use adjacency lists to describe simple graph given in Fig
Example: Represent directed graph shown in Fig
list all vertices that are terminal vertices of edges starting at each
vertex of graph
Adjacency Matrices
Suppose that G = (V ,E) is a simple graph where |V| = n.
Suppose that the vertices of G are listed arbitrarily as v1, v2, . . . , vn.
The adjacency matrix A (or AG) of simple graph or directed graph G,
with respect to this listing of the vertices, is the n x n zero–one matrix

Adjacency matrices can also be used to represent undirected graphs with


loops and with multiple edges.
A loop at vertex vi is represented by a 1 at the (i, i)th position of adjacency
matrix. When multiple edges connecting same pair of vertices vi and vj, or
multiple loops at 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 {vi , vj }.
All undirected graphs, including simple graph (has no loops or multi edges),
multigraphs and pseudographs, have symmetric adjacency matrices Aij = Aji
Example: Use an adjacency matrix to represent graph shown in Fig

Example: Use an adjacency matrix to represent pseudograph shown in Fig


The adjacency matrix for a directed graph does not have to be
symmetric
Adjacency matrices can also be used to represent directed
multigraphs. Again, such matrices are not zero–one matrices when
there are multiple edges in same direction connecting two vertices.
Example: Use an adjacency matrix to represent the directed graph
shown in Fig
Incidence Matrices
Let 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 matrix M = [mij ], where

Multiple 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 loop
Example: Represent graph shown in Fig with an incidence matrix.

Example: Represent pseudograph shown in Fig using an incidence


matrix
Search Techniques
Search Strategies
• Once we have formulated the problem, we have to decide how to
find the solutions to those problems. This could be done by
searching the state space.

• When searching for a solution, you have start at the initial state of
the search tree/graph and expand each state and visit connected
states until the goal state has been reached.

• There are many ways to determine which state to expand in order


to get to the goal state, which are known as ‘Search strategies’.

• These strategies can be mainly divided in to two groups known as


‘Uninformed search’ and ‘Informed search’
Breadth First Search (BFS)
- BFS expands all the nodes at a given depth in the search tree before
any nodes at the next level are expanded
- Que is used in implementation of BFS (FIFO strategy)

BFS-tree Algorithm (Start Node)


Mark all the nodes as NOT visited
Mark the starting node as visited and enqueue it to QUEUE
Make the starting node as the ROOT of BFS-tree
while QUEUE is not empty:
Dequeue the first node F from QUEUE
Expand node F
For each unvisited child of F
Add the child to its parent in the BFS-tree
Mark the child as visited and enqueue it to QUEUE.
End
end
Example What is the BFS tree, BFS path of the graph if the starting
node is A?
[Note: If start point is not given,
take minimum node, start]

[visit all in order]

BFS path=ABDECGFHI
BFS Search Algorithm (Start Node, Goal Node)
Mark all the nodes as NOT visited
Mark the starting node as visited and enqueue it to QUEUE
Make the starting node as the root of BFS-tree
IF the starting node is a goal node, EXIT on success
while QUEUE is not empty:
Dequeue the first node F from QUEUE
Expand node F
For each unvisited child of F
Add the child to its parent in the BFS-tree
Mark the child as visited and enqueue it to QUEUE.
IF the child is a goal node, EXIT on success
End
end
The SEARCH RESULT is the path from the ROOT to the GOAL in the BFS-tree

Example What is the BFS tree, BFS path of the graph if the starting node is A and
goal node is G?

[visit all
in order]

BFS path = ABDECG


Depth First Search (DFS)
- DFS always expands the deepest node in the current frontier of the
search tree.
- Stack is used in implementation of DFS (LIFO strategy)
DFS-tree Algorithm (Start Node)
Mark all the nodes as NOT visited
Mark the starting node as visited and PUSH it to STACK
Make the starting node as the ROOT of DFS-tree
while STACK is not empty:
Expand the top node F from STACK
IF all children of the top node F are visited THEN POP it from STACK
ELSE
Select one unvisited child of F, mark it as visited and PUSH it
to STACK.
Add selected child to its parent (top node) in the DFS-tree.
END
End
Example What is the DFS tree, DFS path of the graph if the starting
node is A? [Note: If start point is not
given, take minimum node, start]

[visit
min node]

DFS path=ABCFEGDHI
DFS Search Algorithm (Start Node, Goal Node)
Mark all the nodes as NOT visited
Mark the starting node as visited and PUSH it to STACK
Make the starting node as the ROOT of DFS-tree
while STACK is not empty:
IF the top node F is a goal node, THEN EXIT on success
Expand the top node F from STACK
IF all children of the top node F are visited THEN POP it from STACK
ELSE
Select one unvisited child of F, mark it as visited and PUSH it to STACK.
Add the selected child to its parent (top node) in the DFS-tree.
END
end
The SEARCH RESULT is the path from the ROOT to the GOAL in the DFS-tree.

Example What is the DFS search output of the following graph if the start node is A and
goal node is G?

[visit min node]

DFS path=ABCFEG
Example What is the BFS tree, BFS path, DFS tree, DFS path of the graph?

Start C

Start C and goal G


Example What is the BFS tree, BFS path, DFS tree, DFS path of the graph?

Start C

Start C and goal G


Example: Find path traversed using BFS and DFS traversal, if start
node is 0, Draw BFS/DFS tree
Shortest Path using Dijkstra’s Algorithm
Dijkstra’s algorithm finds the length of a shortest path between two vertices in a
connected simple undirected weighted graph.
1. It maintains a list of unvisited vertices.
2. It chooses a vertex (source) with cost zero and assigns a maximum possible
cost (i.e. infinity) to every other vertex.
3. In every subsequent step of the algorithm it tries to improve (minimize) the
cost for each vertex.
- For each unvisited vertex of the current vertex calculate the new cost from
the vertex, choose minimum
4. When all the neighbors of the current node are considered, it marks the
current node as visited and is removed from the unvisited list.
5. Select a vertex from the list of unvisited nodes (which has smallest cost) and
repeat step 3
6. At the end there will be no possibilities to improve it further and then the
algorithm ends
Example: What is the length of a shortest
path between a and z in the weighted
graph shown in Fig using Dijkstra’s
Algorithm
What is the length of a shortest path between C and E in the
weighted graph using Dijkstras algorithm

You might also like