Unit 6 Exploring Graphs.pptx
Unit 6 Exploring Graphs.pptx
1
UNIT:6:-
Exploring Graphs
2
Introduction
3
Introduction
4
Types of Graphs:
1. Null Graph: A null graph is defined as a graph which consists only the isolated
vertices.
5
Direct Graph
⚫ In a directed graph. A directed graph is simple if there is
at most one edge from one vertex to another. A directed
graph that has multiple edges from some vertex u to
some other vertex v is called a directed multi-graph.
6
Undirected graph
In an undirected graph, each edge is a two-element subset of
V. A simple undirected graph contains no duplicate edges
and no loops (an edge from some vertex u back to itself).
A graph with more than one edge between the same two
vertices is called a multi-graph. Most of the time, when we
say graph, we mean a simple undirected graph.
7
Direct and Undirected graph
8
Cyclic and Acyclic Graph
9
Self loop
10
Weighted Graph
11
Sparse Graph
⚫ Graph G=(V,E) is called sparse graph if it contains very few edges. In sparse
Graph
⚫ In sparse graph, number of edges are near to minimal number of edges. Sparse
graph is efficiently represented using linked list.
12
Dense Graph
⚫ Graph G=(V,E)is called dense graph if numbers of edges are near to maximum
possible edges.
⚫ Typically, in dense graph, E= O(V2). Dense graph is best describe by adjacency
matrix representation.
13
Tree
⚫ Tree T=(V,’E’) is subset of graph G=(V,E), where V’ is subset of V and E’ is subset
of E. Tree doses not contain any cycle, while graph or sub graph many contain
cycle.
⚫ Graph can have multiple tree as show in fig.
14
Binary Tree Data Structure
⚫ A tree whose elements have at most 2 children is called a binary tree. Since each
element in a binary tree can have only 2 children, we typically name them the left
and right child.
15
Binary Tree Data Structure
Root
It is the topmost node of a tree.
16
Representation of Graph:
⚫ Adjacency-list representation
⚫ Adjacency-matrix representation
17
⚫ Representation of Graph: (Undirected Graph)
⚫ Adjacency-list representation
18
⚫ Representation of Graph: (Undirected graph)
⚫ Adjacency-matrix representation
19
⚫ Representation of Graph: (Directed Graph)
⚫ Adjacency-list representation
20
⚫ Representation of Graph: (Directed graph)
⚫ Adjacency-matrix representation
21
22
Advantage of Graph
⚫ Simple to represent
⚫ Easy to implement
23
Disadvantages
⚫ Require more memory to represent graph.
24
Application of Graph Search:
⚫ Web Crawling
⚫ Social Networking
⚫ Network broadcast routing
⚫ Garbage collection
⚫ Model Checking
⚫ Solving puzzles and game
25
Traversing Graph/Tree
1) Preorder
⚫ To traverse the tree in preorder, perform the following steps:
⚫ i) Visit the root.
⚫ ii) Traverse the left sub tree in preorder.
⚫ iii) Traverse the right sub tree in preorder.
2) In order
⚫ i) Traverse the left sub tree in in order.
⚫ ii) Visit the root.
⚫ iii) Traverse the right sub tree in in order.
3) Post order
⚫ i) Traverse the left sub tree in Post order.
⚫ ii) Traverse the right sub tree in Post order.
⚫ iii) Visit the root
26
Topological Sort
⚫ A topological sort of the nodes of a directed acyclic graph is the operation of
arranging the nodes in order in such a way that if there exists an edge (i, j ), then
i precedes j in the list.
⚫ Given graph (V, E) find a linear ordering of vertices such that for all edges (v,w) v
represents w in ordering.
27
Topological Sort
⚫ Example : Consider the following ⚫ Step-1: Identifying vertices that have no
graph incoming edge. Here, node 5 having no
incoming edges.
⚫ Step-2: Select vertex delete this vertex
with all its outgoing edges and put it in
output list. Here, vertex 5 is deleted.
28
Topological Sort
Explanation: The first vertex in topological sorting is
always a vertex with an in-degree of 0 (a vertex with no
incoming edges). A topological sorting of the
following graph is “5 4 2 3 1 0”. There can be more
than one topological sorting for a graph.
Another topological sorting of the following graph is “4
5 2 3 1 0”.
29
Articulation Point
⚫ Articulation point is the vertex v is V
in graph G=(V,E), whose removal
disconnects graph G. Articulation
point of failure network.it is useful
to identify articulation points in
network to make the network more
reliable.
⚫ If any points we remove then fail all
network its point is called
articulation point.
30
Application of articulation point.
⚫ Articulation point is used to determine single point failure in network.
⚫ It is used to identify bi-connected components of graph.
⚫ Articulation point analysis is useful in graph representation communication
network, example traffic routers, network wires, reservation hubs
31
Depth-First Search of graph
⚫ Let G = (N, A) be an undirected graph all of whose nodes we wish to visit.
⚫ Suppose it is somehow possible to mark a node to show it has already been
visited.
⚫ To carry out a depth-first traversal of the graph, choose any node v Є N as the
starting point.
⚫ Mark this node to show it has been visited.
⚫ Next, if there is a node adjacent to v that has not yet been visited, choose this
node as a new starting point and call the depth-first search procedure
recursively.
32
Depth-First Search of graph
⚫ On return from the recursive call, if there is another node adjacent to v that has
not been visited, choose this node as the next starting point, and call the
procedure recursively once again, and so on.
⚫ When all the nodes adjacent to v are marked, the search starting at v is finished.
⚫ If there remain any nodes of G that have not been visited, choose any one of
them as a new starting point, and call the procedure yet again.
⚫ Continue thus until all the nodes of G are marked.
⚫ Here is the recursive algorithm.
33
Example
1. dfs(1) initial call
2. dfs(2) recursive call
3. dfs(3) recursive call
4. dfs(6) recursive call
5. dfs(5) recursive call : process
is blocked
6. dfs(4) A neighbor of node 1
has not been visited
7. dfs(7) recursive call
8. dfs(8) recursive call
9. There are no more nodes to visit.
Recursive call : process is blocked.
34
Breadth-First Search of graph Algorithm
⚫ Given a graph G = (V, E) and a distinguished source vertex s, breadth-first search
systematically explores the edges of G to "discover" every vertex that is
reachable from s.
⚫ It computes the distance (smallest number of edges) from s to each reachable
vertex.
⚫ It also produces a "breadth-first tree" with root s that contains all reachable
vertices.
⚫ For any vertex v reachable from s, the path in the breadth-first tree from s to v
corresponds to a "shortest path" from s to v in G, that is, a path containing the
smallest number of edges.
35
Breadth-First Search of graph Algorithm
⚫ The algorithm works on both directed and undirected graphs.
⚫ Breadth-first search is so named because it expands the frontier between
discovered and undiscovered vertices uniformly across the breadth of the
frontier.
⚫ That is, the algorithm discovers all vertices at distance k from s before
discovering any vertices at distance k + 1.
⚫ Breadth first search in not naturally recursive. To understand the differences and
similarities, let us first consider the non-recursive formulation of DFS algorithm.
⚫ Let stack be a data type allowing two operations PUSH and POP. It handles
elements in LIFO odder.
36
Breadth-First Search of graph Algorithm
⚫ If node 1 is starting point
Node Visited Q
1. 1 2,3,4
2. 2 3,4,5,6
3. 3 4,5,6
4. 4 5,6,7,8
5. 5 6,7,8
6. 6 7,8
7. 7 8
8. 8 -
37
Difference Between BFS and DFS
PARAMETER BFS DFS
S
Basics BFS stands for Breath First Search. DFS stands for Depth First Search.
Definition BFS is a vertex based technique for finding a DFS is a edge based technique for finding a shortest path in
shortest path in graph. graph.
Data Structure Uses QUEUE data structure for finding the Uses STACK data structure for finding the shortest path.
shortest path.
Structure of Wide and short Narrow and long
constructed tree
Memory Inefficient Efficient
Consumption
Backtracking BFS does not use the backtracking concept. DFS uses backtracking to traverse all the unvisited nodes.
Speed BFS is slower than DFS. DFS is faster than BFS.
Number of edges Finds the shortest path having a minimum A greater number of edges are required to traverse from
number of edges to traverse from source to source vertex to destination vertex.
destination vertex.
Optimality BFS traversal is optimal for those vertices which DFS traversal is optimal for those graphs in which solutions
are to be searched closer to the source vertex. are away from the source vertex. 38
Thank You
39