Lecture 4
Lecture 4
Lecture 4
CS3610
Select those of them that satisfy all the constraints, and then
Find a desired element (e.g., the one that optimizes some objective
function).
Advantages:
widely applicable
Easy
1. Exhaustive search
Find the shortest tour through a given set of n cities that visits each city exactly
once before returning to the city where it starts.
1. Exhaustive search
Given n objects, each object i has weight wi and value vi, and a knapsack of
capacity W
Find most valuable items that fit into the knapsack
1. Exhaustive search
2 5 $30
3 10 $50
4 5 $10
Solution: Consider every possible subset of items, calculate total value and total
weight and discard if more than W; then choose remaining subset with
maximum total value.
Analysis
Input size: n
Running time: C(n) = 2n (number of subsets including Φ)
1. Exhaustive search
Solution: compute all permutations and choose the one with least cost
1. Exhaustive search
Analysis
Input size: n
Running time: C(n) = n!
1. Exhaustive search
Strengths
Wide applicability
Simplicity
Yields reasonable algorithms for some important problems (e.g., matrix
2. Summary
V = set of vertices E
Adjacency: For each edge {u, v} the vertices u and v are said to be adjacent to
one another, denoted u ~ v.
Graph Order: number of vertices |V|
3. Graph traversal
In-degree
Out-degree
Weighted graph: associates weights with either the edges or the vertices
Edges: hyperlinks
3. Graph traversal
Edges: roads
3. Graph traversal
Example:
A 1 2 3 4
1 1 0 1 1 0
2 4 2 0 0 1 0
3 0 0 0 0
3 4 0 0 1 0
Example:
3. Graph traversal
1 Adj[1] = {2,3}
Adj[2] = {3}
2 4 Adj[3] = {}
Adj[4] = {3}
3
Comparison
Exhaustive search can also be applied to two very important algorithms that
systematically process all vertices and edges of a graph.
Depth-First Search (DFS)
4. DFS and BFS
Visiting a vertex
Exploration of a vertex
4. DFS and BFS
Depth-first search: 1, 2, 3, 6, 7, 4, 5
Breadth-first search : 1, 2, 4, 5, 3, 6, 7 1
2 4 5
3 6 7
Analyzing networks
Mapping routes
4. DFS and BFS
Scheduling
Finding spanning trees
DFS can also be used as a subroutine to solve complex problems
Matching algorithm
Hopcroft–Karp
Push a vertex onto the stack when the vertex is reached for the first time (i.e
the visit of the vertex starts)
4. DFS and BFS
Pop a vertex off the stack when it becomes a dead end (i.e., the visit of the
vertex ends).
0 3
Visited
2
4. DFS and BFS
Stack
1 4
0 3
0 Visited
2
4. DFS and BFS
1 2 3 Stack
1 4
0 3
0 1 Visited
2
4. DFS and BFS
2 3 Stack
1 4
0 3
0 1 2 Visited
2
4. DFS and BFS
4 3 Stack
1 4
0 3
0 1 2 4 Visited
2
4. DFS and BFS
3 Stack
1 4
0 3
0 1 2 4 3 Visited
2
4. DFS and BFS
Stack
1 4
A standard DFS implementation puts each vertex of the graph into one of two
categories:
Visited
4. DFS and BFS
Not Visited
The purpose of the algorithm is to mark each vertex as visited while avoiding
cycles.
Take the top item of the stack and add it to the visited list.
4. DFS and BFS
Create a list of that vertex's adjacent nodes. Add the ones which aren't in the
visited list to the top of the stack.
Keep repeating steps 2 and 3 until the stack is empty.
Output: Graph G with its vertices marked with consecutive integers in the order
4. DFS and BFS
DFS(G)
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)
Visit 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
dfs(v)
count ←count + 1;
4. DFS and BFS
Time complexit
4. DFS and BFS