Some Graph Algorithms.

Slides:



Advertisements
Similar presentations
Single Source Shortest Paths
Advertisements

Chapter 9: Graphs Topological Sort
Weighted graphs Example Consider the following graph, where nodes represent cities, and edges show if there is a direct flight between each pair of cities.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
CSE 373, Copyright S. Tanimoto, 2001 Graphs Graphs 2 Incidence and Adjacency Representing a graph with an adjacency matrix, an incidence matrix,
Topological Sort Example This job consists of 10 tasks with the following precedence rules: Must start with 7, 5, 4 or 9. Task 1 must follow 7. Tasks 3.
Topological Sort and Hashing
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Advanced Data Structures
SEARCHING, SORTING, TOPOLOGICAL SORTS Most real world computer applications deal with vast amounts of data. Searching for a particular data item can take.
CSL758 Instructors: Naveen Garg Kavitha Telikepalli Scribe: Manish Singh Vaibhav Rastogi February 7 & 11, 2008.
Shortest Paths Definitions Single Source Algorithms –Bellman Ford –DAG shortest path algorithm –Dijkstra All Pairs Algorithms –Using Single Source Algorithms.
Spanning Trees.
Spanning Trees. 2 Spanning trees Suppose you have a connected undirected graph Connected: every node is reachable from every other node Undirected: edges.
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.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Using Search in Problem Solving
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
Shortest Paths Definitions Single Source Algorithms
 Last lesson  Graphs  Today  Graphs (Implementation, Traversal)
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Spanning Trees. Spanning trees Suppose you have a connected undirected graph –Connected: every node is reachable from every other node –Undirected: edges.
Dynamic Sets and Data Structures Over the course of an algorithm’s execution, an algorithm may maintain a dynamic set of objects The algorithm will perform.
Minimum Spanning Tree in Graph - Week Problem: Laying Telephone Wire Central office.
Shortest Path Algorithms. Kruskal’s Algorithm We construct a set of edges A satisfying the following invariant:  A is a subset of some MST We start with.
1 GRAPHS - ADVANCED APPLICATIONS Minimim Spanning Trees Shortest Path Transitive Closure.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Minimum Spanning Trees
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
Graph Coloring Solution in a Deterministic Machine The deterministic solution to coloring problem uses these steps to assign colors to vertices: 1- Given.
Graphs. 2 Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Discussion #32 1/13 Discussion #32 Properties and Applications of Depth-First Search Trees.
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.
Dijkstra’s Algorithm Supervisor: Dr.Franek Ritu Kamboj
Relations and their Properties
Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
COSC 2007 Data Structures II
Properties and Applications of Depth-First Search Trees and Forests
1 Closures of Relations Based on Aaron Bloomfield Modified by Longin Jan Latecki Rosen, Section 8.4.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
CS 361 – Chapter 13 Graph Review Purpose Representation Traversal Comparison with tree.
Breadth-First Search (BFS)
Graphs A New Data Structure
Directed Graphs 12/7/2017 7:15 AM Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia,
Top 50 Data Structures Interview Questions
Aaron Bloomfield CS 202 Rosen, section 7.4
CSC317 Shortest path algorithms
Topological Sort In this topic, we will discuss: Motivations
Depth-First Search.
Paul Beame in lieu of Richard Anderson
"Learning how to learn is life's most important skill. " - Tony Buzan
CSE 373 Data Structures and Algorithms
Spanning Trees.
Topological Sort CSE 373 Data Structures Lecture 19.
Lectures on Graph Algorithms: searching, testing and sorting
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Minimum Spanning Tree.
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
EE5900 Advanced Embedded System For Smart Infrastructure
CSE 417: Algorithms and Computational Complexity
Some Graph Algorithms.
Closures of Relations Epp, section 10.1,10.2 CS 202.
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Directed Graphs (Part II)
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Some Graph Algorithms

1. Topological sort Suppose a project involves doing a number of tasks, but some of the tasks cannot be done until others are done Your job is to find a legal order in which to do the tasks For example, assume: A must be done before D B must be done before C, D, or E D must be done before H E must be done before D or F F must be done before C H must be done before G or I I must be done before F This is a partial ordering of the tasks Some possible total orderings: A B E D H I F C G B A E D H G I F C A B E D H I G F C

Informal algorithm Extracting a total ordering from a partial ordering is called a topological sort Here’s the basic idea: Repeatedly, Choose a node all of whose “predecessors” have already been chosen A B C D E F G H I A B C F D E I G H Example: Only A or B can be chosen. Choose A. Only B can be chosen. Choose B. Only E can be chosen. Choose E. Continue in this manner until all nodes have been chosen. If all your remaining nodes have predecessors, then there is a cycle in the data, and no solution is possible

Implementing topological sort The graph structure can be implemented in any convenient way We need to keep track of the number of in-edges at each node Whenever we choose a node, we need to decrement the number of in-edges at each of its successors Since we always want a node with the fewest (zero) in-edges, a priority queue seems like a good idea To remove an element from a priority queue and reheap it takes O(log n) time There is a better way

Using buckets We can start with an array of linked lists; array[n] points to the linked list of nodes with n in-edges At each step, Remove a node N from array[0] For each node M that N points to, Get the in-degree d of node M Remove node M from bucket array[d] Add node M to bucket array[d-1] Quit when bucket array[0] is empty As always, it doesn’t make sense to use a high efficiency (but more complex) algorithm if the problem size is small

Bucket example Buckets: 0 → A B 1 → E G H I 2 → C F 3 → D Buckets after choosing B: 0 → A E 1 → G H I C 2 → F D 3 →

2. Connectivity Suppose you want to find out quickly (O(1) time) whether it is possible to get from one node to another in a directed graph You can use an adjacency matrix to represent the graph A B C D E F G A B C D E F G A B C D E F G A B C D E F G A B G E F D C A connectivity table tells us whether it is possible to get from one node to another by following one or more edges

Transitive closure Reachability is transitive: If you can get from A to E, and you can get from E to G, then you can get from A to G A B C D E F G A B C D E F G A B C D E F G A B C D E F G new Warshall’s algorithm is a systematic method of finding the transitive closure of a graph

Warshall’s algorithm Transitivity: If you can get from A to B, and you can get from B to C, then you can get from A to C Warshall’s observation: If you can get from A to B using only nodes with indices less than B, and you can get from B to C, then you can get from A to C using only nodes with indices less than B+1 Warshall’s observation makes it possible to avoid most of the searching that would otherwise be required

Warshall’s algorithm: Implementation for (i = 1; i <= N; i++) { for (j = 1; j <= N; j++) { if (a[j][i]) { for (k = 1; k <= N; k++) { if (a[i][k]) a[j][k] = true; } } } } It’s easy to see that the running time of this algorithm* is O(N3) *Algorithm adapted from Algorithms in C by Robert Sedgewick

3. All-pairs shortest path Closely related to Warshall’s algorithm is Floyd’s algorithm Idea: If you can get from A to B at cost c1, and you can get from B to C with cost c2, then you can get from A to C with cost c1+c2 Of course, as the algorithm proceeds, if you find a lower cost you use that instead The running time of this algorithm is also O(N3)

State graphs The next couple of algorithms are for state graphs, in which each node represents a state of the computation, and the edges between nodes represent state transitions Example: Thread states in Java ready waiting running dead start The edges should be labelled with the causes of the state transitions, but in this example they are too verbose

4. Automata Automata are a formalization of the notion of state graphs Each automaton has a start state, one or more final states, and transitions between states The start state A state transition a A final state

Operation of an automaton An automation represents a “program” to accept or reject a sequence of inputs It operates as follows: Start with the “current state” set to the start state and a “read head” at the beginning of the input string; While there are still characters in the string: Read the next character and advance the read head; From the current state, follow the arc that is labeled with the character just read; the state that the arc points to becomes the next current state; When all characters have been read, accept the string if the current state is a final state, otherwise reject the string.

Example automaton Example input string: 1 0 0 1 1 1 0 0 q0 q1 q2 q3 1 Example input string: 1 0 0 1 1 1 0 0 Sample trace: q0 1 q1 0 q3 0 q1 1 q0 1 q1 1 q0 0 q2 0 q0 Since q0 is a final state, the string is accepted

Example automaton A “hard-wired” automaton is easy to implement in a programming language state := q0; loop case state of q0 : read char; if eof then accept string; if char = 0 then state := q2; if char = 1 then state := q1; q1 : read char; if eof then reject string; if char = 0 then state := q3; if char = 1 then state := q0; q2 : read char; if eof then reject string; if char = 0 then state := q0; if char = 1 then state := q3; q3 : read char; if eof then reject string; if char = 0 then state := q1; if char = 1 then state := q2; end case; end loop; q0 q1 q2 q3 1 • A non-hard-wired automaton can be implemented as a directed graph

5. Nondeterministic automata A nondeterministic automaton is one in which there may be more than one out-edge with the same label A C B a etc. A nondeterministic automaton accepts a sequence of inputs if there is any way to use that string to reach a final state There are two basic ways to implement a nondeterministic automaton: Do a depth-first search, using the inputs to choose the next state Keep a set of all the states you could be in; for example, starting from {A} with input a, you would go to {B, C}

6. String searching Automata can be used to implement efficient string searching Example: Search ABAACAAABCAB for ABABC 1 2 5 3 A B A B C A * 4 The “*” stands for “everything else”

The End