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

Slides:



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

Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Graph Theory, DFS & BFS Kelly Choi What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Algorithms and Data Structures
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Breadth-First and Depth-First Search
Chapter 8, Part I Graph Algorithms.
Chapter 5 Decrease and Conquer. Homework 7 hw7 (due 3/17) hw7 (due 3/17) –page 127 question 5 –page 132 questions 5 and 6 –page 137 questions 5 and 6.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Alyce Brady CS 510: Computer Algorithms Breadth-First Graph Traversal Algorithm.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Alyce Brady CS 510: Computer Algorithms Depth-First Graph Traversal Algorithm.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Tree Searching Breadth First Search Dept First Search.
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs A New Data Structure
Brute Force II.
Graphs Breadth First Search & Depth First Search
Graphs Chapter 20.
Elementary Graph Algorithms
Thinking about Algorithms Abstractly
Graphs Representation, BFS, DFS
CSE 373 Topological Sort Graph Traversals
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Graph Searching.
Depth-First Search.
Graphs Breadth First Search & Depth First Search
CS120 Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graph & BFS.
Graphs Representation, BFS, DFS
Elementary Graph Algorithms
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Graph Traversal Lecture 18 CS 2110 — Spring 2019.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
A vertex u is reachable from vertex v iff there is a path from v to u.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Analysis and design of algorithm
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

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

Lecture #4 (c) algorithm design strategies: Brute Force and Exhaustive Search Lecture #4 (c) algorithm design strategies: Brute Force and Exhaustive Search

Depth-First Search and Breadth-First Search

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.

Graph representation – undirected graphAdjacency listAdjacency matrix

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

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

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

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)

Depth-First Search A BC DEFG A B D E F C G

CS 473Lecture 1411 Depth-First Search: Example DFS(G) terminatedDepth-first forest (DFF)

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 )

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

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.

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.

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

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

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)

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