# Analysis of Algorithms Depth First Search. Graph A representation of set of objects Pairs of objects are connected Interconnected objects are called “vertices.

## Presentation on theme: "Analysis of Algorithms Depth First Search. Graph A representation of set of objects Pairs of objects are connected Interconnected objects are called “vertices."— Presentation transcript:

Analysis of Algorithms Depth First Search

Graph A representation of set of objects Pairs of objects are connected Interconnected objects are called “vertices or nodes” Links that connects the vertices are called “edges or lines” Graph is an ordered pair G=(V, E), where V: a set of vertices E: a set of edges

Undirected & Directed Graphs Undirected: This graph has no orientation Edges are not ordered pairs i.e. (a,b) is identical to (b,a). Maximum no. of edges are n(n-1)/2

Directed: Directed graph is also called “Digraph” It is an ordered pair D=(V,A), where V is a set of vertices A is a set of ordered pairs of vertices also called arcs.

Graph Traversal Graph traversal: The problem of visiting all the nodes in a graph in a particular mannergraph updating and/or checking their values along the way Different algorithms exist for this purpose including BFS and DFS

Tree Search A tree search starts at the root and explores nodes from there, looking for a goal node (a node that satisfies certain conditions, depending on the problem) For some problems, any goal node is acceptable ( N or J ); for other problems, you want a minimum-depth goal node, that is, a goal node nearest the root (only J )

LM N OP G Q H J IK FED BC A Goal nodes

A depth-first search (DFS) explores a path all the way to a leaf before backtracking and exploring another path For example, after searching A, then B, then D, the search backtracks and tries another path from B Node are explored in the order A B D E H L M N I O P C F G J K Q N will be found before J LM N OP G Q H J IK FED BC A

ALGORITHM DFS(G) //Input: Graph G = V, E //Output: Graph G with its vertices marked with consecutive //integers in the order they are first encountered by the DFS //traversal mark each vertex in V with 0 as a mark of being “unvisited” count ←0 for each vertex v in V do if v is marked with 0 dfs(v) count ←count + 1; mark v with count for each vertex w in V adjacent to v do if w is marked with 0 dfs(w)

DFS, How it works Starts at an arbitrary vertex by marking it as visited. On each iteration, the algorithm proceeds to an unvisited vertex that is adjacent to the one it is currently in. If several vertices exist, a tie can be resolved arbitrarily. The process continues until a dead end (a vertex with no adjacent unvisited vertices) is encountered.

At a dead end, the algorithm backs up one edge to the vertex it came from and tries to continue visiting unvisited vertices from there. The algorithm eventually halts after backing up to the starting vertex

a c d f e g b h i j

Download ppt "Analysis of Algorithms Depth First Search. Graph A representation of set of objects Pairs of objects are connected Interconnected objects are called “vertices."

Similar presentations