Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 410 Applied Algorithms Applied Algorithms Lecture #10 Graph Traversal.

Similar presentations


Presentation on theme: "CS 410 Applied Algorithms Applied Algorithms Lecture #10 Graph Traversal."— Presentation transcript:

1 CS 410 Applied Algorithms Applied Algorithms Lecture #10 Graph Traversal

2 CS 410 Applied Algorithms Announcements Masseh College of Engineering –Recognition Event –Third floor of Smith Center. –Today Friday 6/3/05 starting at 3:30pm ACM Student Chapter Meeting –Where: FAB-150. Fourth Avenue Building, across the hallway from the Linux Lab – When: Wednesday, June 1. 11:30AM – Topic: Become an Undergrad Researcher at PSU! Final Exam Date –Monday, June 6, 2005 –12:30 – 14:20 PM

3 CS 410 Applied Algorithms Bigger Square This is definitely a backtracking search problem!

4 CS 410 Applied Algorithms 0 1 2 3 4 5 6 7 8 9 10 11 12 13

5 CS 410 Applied Algorithms Some Puzzles are Easy!

6 CS 410 Applied Algorithms Puzzles with an even side length always have minimal solutions with 4 tiles What about a puzzle with side length = 15 ?

7 CS 410 Applied Algorithms

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 CS 410 Applied Algorithms 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 CS 410 Applied Algorithms Primes Why are primes hard?

11 CS 410 Applied Algorithms Strategy Lay down big squares first. What positions are possible?

12 CS 410 Applied Algorithms 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 CS 410 Applied Algorithms Try Size 7 Then size 6 We tried 5 But found better solutions with 4

14 CS 410 Applied Algorithms Backtracking

15 CS 410 Applied Algorithms Rules For a square of size n In a puzzle of size p g en :: [Square] -> Int -> (Int,Int) -> [Square] gen chosen n (lo,p) = [ Sq n i j | i <- [lo..p-n], j <- [lo..p-n], ok chosen (Sq n i j)] A square of size n, with upper-left corner at position (I,j)

16 CS 410 Applied Algorithms When is it OK to lay down a new square? When it doesn’t 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@(x1,y1)) (p2@(x2,y2)) (p3@(x3,y3)) (p4@(x4,y4)) = (x2 > x3) && (x4 > x1) && (y2 > y3) && (y4 > y1) overlap :: Square -> Square -> Bool overlap (Sq n1 x1 y1) (Sq n2 x2 y2) = rectIntersect (x1,y1) (x1+n1,y1+n1) (x2,y2) (x2+n2,y2+n2)

17 CS 410 Applied Algorithms 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 didn’t lay it down.

18 CS 410 Applied Algorithms This was a hard problem Problem has a straightforward back tracking solution. My solution couldn’t 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 CS 410 Applied Algorithms 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… ab cd

20 CS 410 Applied Algorithms 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 g[i][j]. 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 CS 410 Applied Algorithms Adjacency matrix representation: A simple example: Uses O(|V| 2 ) 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! ab cd

22 CS 410 Applied Algorithms Adjacency list representation A simple example: Uses O(|V|+|E|) 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. ab cd a b c d bcd 0 a a 0 ab 0 d 0

23 CS 410 Applied Algorithms Function representation Best when we have directed graphs. I.e. edges have an orientation. A simple example: list graph(node x) { if (node==a) return [b,c,d] else if (node==b) return [d] else if (node==c) return [] else if (node==d) return [] else return [] } ab cd

24 CS 410 Applied Algorithms 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 (node==a) return [b,c,d] else if (node==b) return [d] else if (node==c) return [] else if (node==d) return [] else return [] } ab cd 3 1 0 0 b d ? ? c ? ? ? d ? 1 ? a b c d int count [5] node child1 [5]node child2 [5] node child3 [5]

25 CS 410 Applied Algorithms With 2-D arrays #define MAXV 100 // maxumum number of edges #define MAXDEGREE 50 // maximum vertex out degree typedef struct { int edges[MAXV+1][MAXDEGREE]; // adjacency info int degree[MAXV+1]; // outdegree of each vertex int nvertices; // number of vertices int nedges; // number of edges } graph; g->edges[x] is an array of successor nodes of node x g->degree[x] the number of successors nodes for x

26 CS 410 Applied Algorithms 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 CS 410 Applied Algorithms 0 1 1 0 1 2 2 2 1 2 2 2 2 3 2 3 Key: Unvisited Discovered Visited

28 CS 410 Applied Algorithms Implementation: color all vertices unvisited; color[s] = 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 (color[v] == unvisited) { color[v] = discovered; enqueue(v); }} color[u] = visited; } Using a FIFO structure ensures that the first vertices discovered are visited before later ones 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 CS 410 Applied Algorithms What if we used a stack? color all vertices unvisited; color[s] = 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 (color[v] == unvisited) { color[v] = discovered; push(v); } color[u] = 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 CS 410 Applied Algorithms Key: Unvisited Discovered Visited 0 0 12 2 2 1 2 2 2 2 3 2 4 3 4

31 CS 410 Applied Algorithms Depth first search (DFS): DFS is usually implemented using recursion, and without an explicit stack (d[t] is discovery time, f[t] is finish time) : color all vertices unvisited; time = 0; for (each vertex v in V) if (color[v]==unvisited) visit(v); where : visit (u) { color[u] = discovered; d[u] = ++time; for (each v in the adjacency list for u) { if (color[v]==unvisited) { parent[v] = u; visit(v); } } color[u] = visited; f[u] = ++time; } Records discovery time Records finish time

32 CS 410 Applied Algorithms 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 CS 410 Applied Algorithms From the text book Bool finished; Bool processed[MAXV]; Bool discovered[MAXV]; int parent[MAXV]; void dfs(graph *g, int v) { int i; // counter int y; // successor vertexes of i, as we go around loop if (finished) return; for (i=0; i degree[v]; i++) { y = g->edges[v][i]; if (valid_edge(g->edges[v][i]) == True) { if (discovered[y] == False) { parent[y] = v; dfs(g,y); } else if (processed[y] == False) process_edge(v,y); } if (finished) return; } processed[v] = True; }

34 CS 410 Applied Algorithms 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/index.html

35 CS 410 Applied Algorithms Today’s Assignments Study for the final exam! There will be some Quiz-like questions from chapter 9 on the exam


Download ppt "CS 410 Applied Algorithms Applied Algorithms Lecture #10 Graph Traversal."

Similar presentations


Ads by Google