Presentation is loading. Please wait.

Presentation is loading. Please wait.

Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.

Similar presentations


Presentation on theme: "Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble."— Presentation transcript:

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 #4 (c) algorithm design strategies: Brute Force and Exhaustive Search Lecture #4 (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. Two vertices are on a path if there is a sequences of vertices beginning with the first one and ending with the second one Graphs with ordered edges are directed. For directed graphs, vertices have in and out degrees. 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 CS 473Lecture 1411 Depth-First Search: Example DFS(G) terminatedDepth-first forest (DFF)

12 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 )

13 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

14 Breadth first search It proceeds in a concentric manner by visiting first all the vertices that are adjacent to a starting vertex, then all unvisited vertices two edges apart from it, and so on, until all the vertices in the same connected omponent as the starting vertex are visited. If there still remain unvisited vertices, the algorithm has to be restarted at an arbitrary vertex of another connected component of the graph.

15 It is convenient to use a queue (note the difference from depth-first search!) to trace the operation of breadth-first search. The queue is initialized with the traversal’s starting vertex, which is marked as visited. On each iteration, the algorithm identifies all unvisited vertices that are adjacent to the front vertex, marks them as visited, and adds them to the queue; after that, the front vertex is removed from the queue.

16 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

17 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

18 18 Directed Depth First Search Adjacency Lists A: F G B: A H C: A D D: C F E: C D G F: E: G: : H: B: I: H: F A B C G D E H I

19 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)

20 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


Download ppt "Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble."

Similar presentations


Ads by Google