Brute Force and Exhaustive Search

advertisement
Brute Force and
Exhaustive Search
Traveling Salesman Problem
 Knapsack Problem
 Assignment Problem
 Selection Sort and Bubble
 Sort Sequential Search
 Depth-First Search and BreadthFirst Search

Lecture #5 (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 = {v1, v2, ….. vn}
set of edges that connect the vertices E ={e1, e2, …. em}
Two vertices in a graph are adjacent if there is an edge
connecting the vertices
Weighted Graphs have values associated with edges.
Graph representation – undirected
graph
Adjacency list
Adjacency 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
B
C

D
E
F
H
L
M
I
N
O
G
J
P
K

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
CFGJKQ
Q

N will be found before J
Breadth-first searching

A
B
C

D
E
F
G

H
I
J
K
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
MNOPQ
L
M
N
O
P
Q

J will be found before N
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)
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
B
D
C
E
F
A B D E F C G
G
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(n2)
Time and Space Complexity
for Depth-First Search

Space Complexity
◦ Stack to handle nodes as they are explored
 Worst case: all nodes put on stack (if graph is linear)
 O(n)
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
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
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)
ref. Introduction to Algorithms by Thomas
Cormen
Download