Applied Algorithms - PowerPoint PPT Presentation

About This Presentation
Title:

Applied Algorithms

Description:

Third floor of Smith Center. Today Friday 6/3/05 starting at 3:30pm. ACM Student Chapter Meeting ... even side length always have minimal solutions with 4 tiles ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 36
Provided by: she46
Learn more at: http://web.cecs.pdx.edu
Category:

less

Transcript and Presenter's Notes

Title: Applied Algorithms


1
Applied Algorithms
  • Lecture 10
  • Graph Traversal

2
Announcements
  • 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

3
Bigger Square
  • This is definitely a backtracking search problem!

4
0 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
5
Some 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)
8
Any 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

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

10
Primes
  • Why are primes hard?

11
Strategy
  • Lay down big squares first.
  • What positions are possible?

12
Strategy
  • 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

14
Backtracking
15
Rules
  • 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)
16
When 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)

17
Pruning 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.

18
This 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.

19
Graphs
  • 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

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

21
Adjacency 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!

22
Adjacency 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.

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

24
Parallel 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
25
With 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

26
Breadth 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.

27
2
0
2
2
3
28
Implementation
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).
29
What 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).
30
2
0
2
2
4
31
Depth 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
32
Depth 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.

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

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

35
Todays Assignments
  • Study for the final exam!
  • There will be some Quiz-like questions from
    chapter 9 on the exam
Write a Comment
User Comments (0)
About PowerShow.com