Title: Sorting
1Sorting Example with Insertion Sort
i 5 A E M P X L E
i 1 E X A M P L E
i 2 E X A M P L E
i 6 A E L M P X E
i 3 A E X M P L E
i 7 A E E L M P X
i 4 A E M X P L E
2Closest Pair Pseudo-code
- ClosestPair(P1..n)
- //input P1..n a collection of points sorted by
the x coordinates - //output Points r and s, with r ? s, such that
distance(r,s) is the shortest distance among all
pairs of points in P
If (n 2) then return (P1,P2)
C1 ? Select(P1..mid,c,d) C2 ?
Select(Pmid1..n,c,d) for each k in C1 do
Cand ? candidates(k,C2,d) //at most 6 points
will be in D for each ca in Cand do if
distance(k,ca) lt d) then d ?
distance(k,ca) (r,s) ?
(k,ca) return (r,s)
mid ? floor(n/2) c ? (Pmid.x
Pmid1.x)/2 (a,b) ? ClosestPair(P1..mid) (w,z)
? ClosestPair(Pmid1..n) d1 ? distance(a,b) d2
? distance(w,z) if (d1 lt d2) then (r,s,d) ?
(a,b,d1) else (r,s,d) ?(w,z,d2)
3Graph Representations
G (E,V)
D
E
A
F
C
B
G
H
Adjacency lists vs. Adjacency Matrix (Pages 29,
30)
V2
V k 2 E
Size
4Depth-First Search
- Initially all nodes are marked with 0 (non
visited). - Every node that is visited will be marked with a
value of 1 or more - The number marking a node indicates its depth
relative to other marked nodes - Complexity
Adjacency Lists O(V E)
Adjacency Matrix O(V2)
5Breadth-First Search
- Initially all nodes are marked with 0 (non
visited). - Every node that is visited will be marked with a
value of 1 or more - Use a queue (FIFO policy) to control order of
visits - Complexity
Adjacency Lists O(V E)
Adjacency Matrix O(V2)
6Questions about Graphs
- Is the graph connected?
- Find the connected components of the graph
- Is the graph acyclic?
- Find articulation points
- Find minimum-number of edges
7Applications of Graphs
- In many problems, the graphs are not constructed
apriori because they would be extremely large. - In such situations while the system is solving a
problem, it is constructing the graph as-needed. - Example of such a problem Computer-generated
Game plan
8Planning Simple Example
A B C
C A
B
Initial
Goal
(on A Table) (on C A) (on B Table) (clear B)
(clear C)
(on C Table) (on B C) (on A B) (clear A)
9Search Space A Graph!
C
A
B
C
A
B
C
B
A
B
A
C
A
B
C
B
A
B
C
B
C
A
B
A
C
C
A
A
A
B
C
B
C
C
B
A
A
B
C
10Homework (Optional)