10.09.2013 Views

1. Advanced Data Structure using C++

1. Advanced Data Structure using C++

1. Advanced Data Structure using C++

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

LECTURE NOTES OF ADVANCED DATA STRUCTURE (MT-CSE 110)<br />

Introduction:‐<br />

A graph traversal means visiting all the nodes of the graph . Graph<br />

traversal may be needed in many of the application areas.<br />

Given the root node of a binary tree, one of the most common<br />

things one wishes to do is to traverse the tree and visit the nodes in some<br />

order. In the case of trees, we have three ways to traverse it i.e.<br />

PREORDER, INORDER and POSTORDER. An analogous problem arises in<br />

the case of Graphs.<br />

Given an undirected graph G= (V, E) and a vertex v in V (G) we are<br />

inserted in visiting all vertices in G that are reachable from v (i.e., all<br />

vertices connected to v). We shall look at two ways of doing this:‐<br />

<strong>1.</strong> Depth First Traversal(Depth First Search)<br />

2. Breadth First Traversal(Breadth First Search)<br />

Both techniques produce a systematic traverse and when properly applied are<br />

useful for checking some basic properties of graphs. Both traverse the graph<br />

along the edges of a forest (spanning tree if the graph is connected).<br />

DFS (Depth first search)<br />

Depth‐first search (DFS) is an algorithm for traversing or searching a tree, tree<br />

structure, or graph. One starts at the root (selecting some node as the root in<br />

the graph case) and explores as far as possible along each branch<br />

before backtracking<br />

DFS is an uninformed search that progresses by expanding the first<br />

child node of the search tree that appears and thus going deeper and<br />

deeper until a goal node is found, or until it hits a node that has no<br />

children. Then the search backtracks, returning to the most recent node it<br />

hasn't finished exploring. In a non‐recursive implementation, all freshly<br />

expanded nodes are added to a stack for exploration.<br />

The time and space analysis of DFS differs according to its<br />

application area. In theoretical computer science, DFS is typically used to<br />

traverse an entire graph, and takes time O(|V| + |E|), linear in the size of<br />

the graph. In these applications it also uses space O(|V| + |E|) in the<br />

Prepared By :­<br />

Er. Harvinder Singh<br />

Assist Prof., CSE, H.C.T.M (Kaithal) Page ‐ 83 ‐

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!