0% found this document useful (0 votes)
17 views16 pages

Graph Treversal (BFS-DFS) Unit 5

The document provides an overview of graph theory, defining graphs, undirected and directed graphs, and weighted graphs. It explains graph traversal methods, specifically Breadth First Search (BFS) and Depth First Search (DFS), detailing their algorithms and pseudocode. The content is prepared by Mr. Zeeshan Mubeen, focusing on the importance of visiting each vertex and edge in a defined order.

Uploaded by

raheelrajpoot886
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views16 pages

Graph Treversal (BFS-DFS) Unit 5

The document provides an overview of graph theory, defining graphs, undirected and directed graphs, and weighted graphs. It explains graph traversal methods, specifically Breadth First Search (BFS) and Depth First Search (DFS), detailing their algorithms and pseudocode. The content is prepared by Mr. Zeeshan Mubeen, focusing on the importance of visiting each vertex and edge in a defined order.

Uploaded by

raheelrajpoot886
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Graph Traversal (BFS and DFS)

Unit No. 6

Prepared By:
Mr. Zeeshan Mubeen (Senior Lecturer, Superior)
Graph
• A Graph is a non-linear data structure consisting of nodes and
edges. The nodes are sometimes also referred to as vertices
and the edges are lines or arcs that connect any two nodes in
the graph.
Undirected Graph

Graph 1:

V = {A, B, C, D, E, F}
E = {(A, B), (A, C), (B, C), (B, D), (D, E), (D, F), (E, F)}
Directed Graph

• If an edge is represented using a pair of vertices (V1, V2), the


edge is said to be directed from V1 to V2.
• The first element of the pair V1 is called the start vertex and
the second element of the pair V2 is called the end vertex.

Set of Vertices V = {1, 2, 3, 4, 5}


Set of Edges W = {(1, 3), (1, 5), (2, 1), (2, 3), (2, 4), (3, 4), (4, 5)}
Weighted Graph
A weighted graph is a graph in which each branch is given a
numerical weight. A weighted graph is therefore a special type
of labeled graph in which the labels are numbers (which are
usually taken to be positive)
Graph traversals:
• Graph traversal means visiting every vertex and edge exactly
once in a well-defined order. While using certain graph
algorithms, you must ensure that each vertex of the graph is
visited exactly once.

• The order in which the vertices are visited are important and
may depend upon the algorithm or question that you are
solving.

The most common ways to travers the graph:


• Breadth First Search (BFS)
• Depth First Search (DFS)
Breadth First Search (BFS)

• BFS is a traversing algorithm where you should start traversing


from a selected node (source or starting node) and traverse
the graph layer wise.

As the name BFS suggests, you are required to traverse the


graph breadthwise as follows:

• First move horizontally and visit all the nodes of the current
layer
• Move to the next layer
BFS Diagram
Traversing process
Traversing process
Traversing process
Breadth First Search (BFS)
Pseudocode:
• BFS (G, s) //Where G is the graph
and s is the source node
• Let Q be queue.
• Q.enqueue( s ) //Inserting s in queue until
all its neighbor vertices are marked.
• mark s as visited.

• while ( Q is not empty)


• v = Q.dequeue( )

• //traversing all the neighbors of v


• for all neighbors w of v in Graph G
• if w is not visited
• Q.enqueue( w ) //Stores w in Q to
further visit its neighbor
• mark w as visited.
DFS
• Depth-first search is an algorithm for traversing or searching
tree or graph data structures. The algorithm starts at the root
node and explores as far as possible along each branch before
backtracking.
Traversing process

Stack
Continue..

Stack
Depth-first search (DFS)
pseudocode:
• DFS(G,v) ( v is the vertex where the search
starts )
• Stack S := {}; ( start with an empty stack )
• for each vertex u, set visited[u] := false;
• push S, v;
• while (S is not empty) do
• u := pop S;
• if (not visited[u]) then
• visited[u] := true;
• for each unvisited neighbor w of u
• push S, w;
• end if
• end while
• END DFS()

You might also like