Download presentation
Presentation is loading. Please wait.
Published byJoshua Daniel Modified over 8 years ago
1
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble SortSequential Search Depth-First Search and Breadth- First Search
2
Lecture #5 (c) algorithm design strategies: Brute Force and Exhaustive Search Lecture #5 (c) algorithm design strategies: Brute Force and Exhaustive Search
3
Depth-First Search and Breadth-First Search
4
Graph terminology - overview A graph consists of : set of vertices V = {v 1, v 2, ….. v n } set of edges that connect the vertices E ={e 1, e 2, …. e m } Two vertices in a graph are adjacent if there is an edge connecting the vertices Weighted Graphs have values associated with edges.
5
Graph representation – undirected graphAdjacency listAdjacency matrix
6
introduction The term “exhaustive search” can also be applied to two very important algorithms that systematically process all vertices and edges of a graph. These two traversal algorithms are depth-first search (DFS) and breadth-first search (BFS). These algorithms have proved to be very useful for many applications involving graphs in artificial intelligence and operations research
7
Depth-first searching 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
8
Breadth-first searching A breadth-first search (BFS) explores nodes nearest the root before exploring nodes further away For example, after searching A, then B, then C, the search proceeds with D, E, F, G Node are explored in the order A B C D E F G H I J K L M N O P Q J will be found before N LM N OP G Q H J IK FED BC A
9
ALGORITHM DFS(G) //Implements a depth-first search traversal of a given graph //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) //visits recursively all the unvisited vertices connected to vertex v //by a path and numbers them in the order they are encountered //via global variable count 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)
10
Depth-First Search A BC DEFG A B D E F C G
11
Time and Space Complexity for Depth-First Search Time Complexity ◦ Adjacency Lists Each node is marked visited once Each node is checked for each incoming edge O (v + e) ◦ Adjacency Matrix Have to check all entries in matrix: O(n 2 )
12
Space Complexity ◦ Stack to handle nodes as they are explored Worst case: all nodes put on stack (if graph is linear) O(n) Time and Space Complexity for Depth-First Search
13
Breadth first search ALGORITHM BFS(G) //Implements a breadth-first search traversal of a given graph //Input: Graph G = V, E //Output: Graph G with its vertices marked with consecutive integers // in the order they are visited by the BFS 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 bfs(v) the front vertex from the queue
14
bfs(v) //visits all the unvisited vertices connected to vertex v //by a path and numbers them in the order they are visited //via global variable count count ← count + 1; mark v with count and initialize a queue with v while the queue is not empty do for each vertex w in V adjacent to the front vertex do if w is marked with 0 count ← count + 1; mark w with count add w to the queue remove
15
ref. Introduction to Algorithms by Thomas Cormen Breadth first search - analysis Enqueue and Dequeue happen only once for each node. - O(V). Sum of the lengths of adjacency lists – θ (E) (for a directed graph) Initialization overhead O(V) Total runtime O(V+E)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.