Title: Applied Algorithms
1Applied Algorithms
- Lecture 10
- Graph Traversal
2Announcements
- Masseh College of Engineering
- Recognition Event
- Third floor of Smith Center.
- Today Friday 6/3/05 starting at 330pm
- ACM Student Chapter Meeting
- Where   FAB-150. Fourth Avenue Building, across
the hallway from the Linux Lab -  When    Wednesday, June 1. 1130AM
-  Topic   Become an Undergrad Researcher at PSU!
- Final Exam Date
- Monday, June 6, 2005
- 1230 1420 PM
3Bigger Square
- This is definitely a backtracking search problem!
40 1 2 3 4 5 6 7 8 9 10 11 12 13
0 1 2 3 4 5 6 7 8 9 10 11 12 13
5Some Puzzles are Easy!
6- Puzzles with an even side length always have
minimal solutions with 4 tiles - What about a puzzle with side length 15 ?
7(No Transcript)
8Any composite number!
- Let the square side be a composite number
- I.e. its not a prime
- Then find its smalles prime factor
- Assume n 15
- Factorizing 15 3 5, smallest prime is 3
- Make a band of this size around edge.
- Edge band has 3 elements (each of 15/3 5 boxes)
- With one big square the size of all its other
factors. - In the case of 15, of size 5
9Try 42
- N 42
- 42 7 3 2
- Band has 2 elements (each of size 21)
- With one biq square of size 21
- The even rule is a special case
10Primes
11Strategy
- Lay down big squares first.
- What positions are possible?
12Strategy
- Lay down big squares first
- Then squares of size 1 less
- Then squares of size 2 less
- Your done when laying down squares of size 1.
Every empty space can be covered by a square of
size 1, so no need to search further.
13- Try Size 7
- Then size 6
- We tried 5
- But found better solutions with 4
14Backtracking
15Rules
- For a square of size n
- In a puzzle of size p
- gen Square -gt Int -gt (Int,Int) -gt Square
- gen chosen n (lo,p)
- Sq n i j
- i lt- lo..p-n
- , j lt- lo..p-n
- , ok chosen (Sq n i j)
A square of size n, with upper-left corner at
position (I,j)
16When is it OK to lay down a new square?
- When it doesnt conflict with any of the squares
already laid down (or chosen) - -- Determine in two rectangles intersect. Where
(p1,p1) are - -- the upper-left and lower-right of rectangle 1,
and (p3,p4) - -- are the upper-left and lower-right of
rectangle 2. - rectIntersect (p1_at_(x1,y1)) (p2_at_(x2,y2))
(p3_at_(x3,y3)) (p4_at_(x4,y4)) - (x2 gt x3) (x4 gt x1) (y2 gt y3) (y4 gt
y1) - overlap Square -gt Square -gt Bool
- overlap (Sq n1 x1 y1) (Sq n2 x2 y2)
- rectIntersect (x1,y1) (x1n1,y1n1) (x2,y2)
(x2n2,y2n2)
17Pruning the search space
- Corners are better than floating. Why?
- Never lay down a square if it makes the solution
bigger than a previous solution. - But whenever you lay down a square have to
explore what would happen if you didnt lay it
down.
18This was a hard problem
- Problem has a straightforward back tracking
solution. - My solution couldnt solve every number up to
size 50 in the time allotted. - The largest prime I could solve was 23 (in about
3 minutes) - Non-primes can be solved instantaneously.
19Graphs
- A graph is a pair (V,E) where
- V is a set of vertices
- E is a set of edges u,v, where u,v are distinct
vertices from V. - For example
- G (a,b,c,d, a,b, a,c, a,d, b,d)
- Examples computer networks, street layout, etc
20Representing graphs
- Function Define a function that when applied to
vertex v, returns a set of children (or a set of
parents). Each child is a node where (v,c) is in
the set of edges. - Adjacency list for each vertex v, we store a
linked list of the vertices u that it connects to
by a single edge. - Adjacency matrix a two dimensional array
gij. An entry of 1 means that there is an
edge between vertices i and j. - Pointers actually construct a heap object where
edges are implemented by pointers
21Adjacency matrix representation
- A simple example
- Uses O(V2) space, much of which will be wasted
if the graph is sparse (i.e., relatively few
edges). - Easily adapted to store information about each
edge in the entries of the matrix. - Alternatively, if all we need is 0/1, then a
single bit will do!
22Adjacency list representation
- A simple example
- Uses O(VE) space, good for sparse graphs,
more expensive for dense case (i.e., many edges). - Easily adapted to store information about each
edge in each part of the linked lists. - Testing to see if there is an edge (u,v) is not
O(1) we must search the adjacency list of u for
v.
23Function representation
- Best when we have directed graphs. I.e. edges
have an orientation. - A simple example
- list graph(node x)
- if (nodea) return b,c,d
- else if (nodeb) return d
- else if (nodec) return
- else if (noded) return
- else return
-
24Parallel Arrays
- When a graph has fixed in-degree (or out degree)
the function representation has an especially
nice implementation as a set of parallel arrays. - list graph(node x)
- if (nodea) return b,c,d
- else if (nodeb) return d
- else if (nodec) return
- else if (noded) return
- else return
-
int count 5
node child1 5
node child2 5
node child3 5
25With 2-D arrays
- define MAXV 100 // maxumum number of
edges - define MAXDEGREE 50 // maximum vertex out
degree - typedef struct
- int edgesMAXV1MAXDEGREE // adjacency info
- int degreeMAXV1 // outdegree of each vertex
- int nvertices // number of vertices
- int nedges // number of edges
- graph
- g-gtedgesx is an array of successor nodes
of node x - g-gtdegreex the number of successors nodes
for x
26Breadth first search (BFS)
- What vertices can be reached from some given
starting vertex s? - How long is the shortest path to each one?
- The mechanisms of breadth first search
(level-order traversal) that we saw for trees,
can be adapted to graphs. - The overall effect of BFS is to compute a
spanning tree for the connected component that
contains s.
272
0
2
2
3
28Implementation
Using a FIFO structure ensures that the first
vertices discovered are visited before later ones
- color all vertices unvisited
- colors discovered
- initialize queue, and enqueue s
- while (queue is not empty)
- u remove value at head of queue
- for (each v in adjacency list for u)
- if (colorv unvisited)
- colorv discovered
- enqueue(v)
-
- coloru visited
-
No vertex is recolored white during the search,
so each vertex is enqueued at most once.
Complexity time using queues time scanning
adjacency lists O(V E).
29What if we used a stack?
- color all vertices unvisited
- colors discovered
- initialize stack, and push s
- while (stack is not empty)
- u pop value at head of stack
- for (each v in adjacency list for u)
- if (colorv unvisited)
- colorv discovered
- push(v)
-
- coloru visited
-
-
Complexity still O(V E). The only thing
that changes is the order in which we visit the
vertices! The result is called depth first search
(DFS).
302
0
2
2
4
31Depth first search (DFS)
- DFS is usually implemented using recursion, and
without an explicit stack (dt is discovery
time, ft is finish time) - color all vertices unvisited
- time 0
- for (each vertex v in V)
- if (colorvunvisited)
- visit(v)
- where
- visit (u)
- coloru discovered
- du time
- for (each v in the adjacency list for u)
- if (colorvunvisited)
- parentv u
- visit(v)
-
- coloru visited
Records discovery time
Records finish time
32Depth first forests
- We can apply DFS to any graph, even if it is not
connected. - The result is a depth first forest
- There may be many different depth first forests
for any given graph. Different forests are
obtained by picking different start vertices.
33From the text book
- Bool finished
- Bool processedMAXV
- Bool discoveredMAXV
- int parentMAXV
- void dfs(graph g, int v)
- int i // counter
- int y // successor vertexes of i, as we go
around loop - if (finished) return
- for (i0 i lt g-gtdegreev i)
- y g-gtedgesvi
- if (valid_edge(g-gtedgesvi) True)
- if (discoveredy False)
- parenty v
- dfs(g,y)
-
- else if (processedy False)
process_edge(v,y) -
- if (finished) return
34In Class Problems
- Bicoloring 9.6.1
- Page 203 of the text
- Download the skeleton DFS algorithm
- www.cs.pdx.edu/sheard/course/appliedalg/notes/ind
ex.html
35Todays Assignments
- Study for the final exam!
- There will be some Quiz-like questions from
chapter 9 on the exam