CPE133-Lecture Notes - L7-451-Graphs and Search
CPE133-Lecture Notes - L7-451-Graphs and Search
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)
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
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
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
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
• 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.
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]
[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?
DFS path=ABCFEG
Example What is the BFS tree, BFS path, DFS tree, DFS path of the graph?
Start C
Start C