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

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Depth-First Search1 Part-H2 Depth-First Search DB A C E.
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
Breadth-First and Depth-First Search
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graph.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
CS 410 Applied Algorithms Applied Algorithms Lecture #3 Data Structures.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
COMP171 Depth-First Search.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
CS 410 Applied Algorithms Applied Algorithms Lecture #2 Overall program structure, Representing graphs, finding bugs.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
CS261 Data Structures DFS and BFS – Edge List Representation.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Search Related Algorithms. Graph Code Adjacency List Representation:
MA/CSSE 473 Day 12 Insertion Sort quick review DFS, BFS Topological Sort.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Chapter 2 Graph Algorithms.
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Introduction to Graphs And Breadth First Search. Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Graph. Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 05 Introduction to Graph And Search Algorithms.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
CSC317 Graph algorithms Why bother?
Depth-First Search.
Graphs Breadth First Search & Depth First Search
Graph.
Finding Shortest Paths
Graphs Chapter 11 Objectives Upon completion you will be able to:
Chapter 11 Graphs.
3.2 Graph Traversal.
Elementary Graph Algorithms
Presentation transcript:

CS 410 Applied Algorithms Applied Algorithms Lecture #10 Graph Traversal

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

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

CS 410 Applied Algorithms

CS 410 Applied Algorithms Some Puzzles are Easy!

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 ?

CS 410 Applied Algorithms

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

CS 410 Applied Algorithms Try 42 N = = 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

CS 410 Applied Algorithms Primes Why are primes hard?

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

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.

CS 410 Applied Algorithms Try Size 7 Then size 6 We tried 5 But found better solutions with 4

CS 410 Applied Algorithms Backtracking

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)

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 = (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)

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.

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.

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

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

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

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

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

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 b d ? ? c ? ? ? d ? 1 ? a b c d int count [5] node child1 [5]node child2 [5] node child3 [5]

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

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.

CS 410 Applied Algorithms Key: Unvisited Discovered Visited

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|).

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).

CS 410 Applied Algorithms Key: Unvisited Discovered Visited

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

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.

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; }

CS 410 Applied Algorithms In Class Problems Bicoloring Page 203 of the text –Download the skeleton DFS algorithm –

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