Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Claus Brabrand [ ] ( “FÅP”: First-year Project Course, ITU, Denmark.

Slides:



Advertisements
Similar presentations
What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Advertisements

Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Graphs CSE 331 Section 2 James Daly. Reminders Homework 4 is out Due Thursday in class Project 3 is out Covers graphs (discussed today and Thursday) Due.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph A graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among.
Graphs CS3240, L. grewe.
1 CSE 326: Data Structures: Graphs Lecture 19: Monday, Feb 24, 2003.
Graph.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
Graph & BFS.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Connected Components, Directed Graphs, Topological Sort COMP171.
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.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
Connected Components, Directed Graphs, Topological Sort Lecture 25 COMP171 Fall 2006.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Using Search in Problem Solving
Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ ] IT University of Copenhagen.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
 Last lesson  Graphs  Today  Graphs (Implementation, Traversal)
More Graph Algorithms Weiss ch Exercise: MST idea from yesterday Alternative minimum spanning tree algorithm idea Idea: Look at smallest edge not.
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.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Search Related Algorithms. Graph Code Adjacency List Representation:
Spring 2015 Lecture 10: Elementary Graph Algorithms
Fundamentals, Terminology, Traversal, Algorithms Graph Algorithms Telerik Algo Academy
Graphs. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
Graphs and Graph Algorithms Fundamentals, Terminology, Traversal, Algorithms SoftUni Team Technical Trainers Software University
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
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs Upon completion you will be able to:
Graphs and Paths : Chapter 15 Saurav Karmakar
Graphs and Shortest Paths Using ADTs and generic programming.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
CS 367 Introduction to Data Structures Lecture 13.
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.
CSE373: Data Structures & Algorithms Lecture 14: Topological Sort / Graph Traversals Dan Grossman Fall 2013.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Week 11 - Wednesday.  What did we talk about last time?  Exam 2  And before that:  Graph representations  Depth first search.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Graphs. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
CS 202, Spring 2003 Fundamental Structures of Computer Science II Bilkent University1 Graph Algorithms CS 202 – Fundamental Structures of Computer Science.
Breadth-First Search (BFS)
Topological Sorting.
Graphs Lecture 19 CS2110 – Spring 2013.
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
Csc 2720 Instructor: Zhuojun Duan
Graph Traversals Depth-First Traversals. Algorithms. Example.
More Graph Algorithms.
CSE 373 Data Structures and Algorithms
Topological Sort CSE 373 Data Structures Lecture 19.
Lectures on Graph Algorithms: searching, testing and sorting
Graphs.
Richard Anderson Autumn 2016 Lecture 5
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Winter 2019 Lecture 5
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:

Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Claus Brabrand [ ] ( “FÅP”: First-year Project Course, ITU, Denmark )

[ 2 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms

[ 3 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Outline Graphs Graph Representations Krak Data Representation Breadth First Search (BFS) Depth First Search (DFS) Topological Sorting Exercises Project (part 1): ”Visualization”

[ 4 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Definition: GRAPH A ”graph G = (V,E)” consists of: V: a set of vertices (nodes)(knuder) E: a set of edges (arcs)(kanter) where E  V  V There is an edge between ’v 1 ’ and ’v 2 ’ if (v 1, v 2 )  E(abbreviated: ”v 1  v 2 ”) Example: V = { 1, 2, 3, 4 } E = { (1,2), (2,1), (2,3), (3,1), (4,2) } vertex edge

[ 5 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Oriented vs. Non-oriented Directed graph: (orienteret graf) Undirected graph: (ikke-orienteret) V = { 1, 2, 3, 4 } E = { (1,2), (2,1), (2,3), (3,1), (4,2) } V = { 1, 2, 3, 4 } E = { (1,2), (2,1), (1,3), (3,1), (2,3), (3,2), (2,4), (4,2) } v 1  v 2  v 2  v 1

[ 6 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Graph Examples Roadmap: roads and intersections project! Powergrid: wires and connexions Rivers: river and confluences World Wide Web: pages and links Facebook: people and friends-of Data structures: objects and references Class hierarchies: classes and inheritances Bacon index: people and co-starring

[ 7 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Graph Terminology A path (sti) is a sequence of adjacent edges: p = (e 0, e 1, …, e n ) A cycle (kreds) is a non-empty path from a vertex to itself: p = (e 0, e 1, …, e n ) v 0  v 1  …  v n+1 e0e0 e1e1 enen v 0  v 1  …  v 0 (v 0,_)(_,v 0 ) = = (v 0,v 1 )(v n,v n+1 ) = = (v 1,v 2 ) =

[ 8 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Graph Terminology (cont’d) A directed acyclic graph (DAG) (orienteret acyklisk graf) is a connected graph w/o cycles: A tree (træ) is a connected acyclic graph every node has indegree 0 or 1 (i.e., a DAG w/o ”sharing”):

[ 9 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Graph Properties A graph ”G = (V,E)” is: Reflexive: Symmetric: Transitive:  v 1  V: v 1  v 1  v 1,v 2  V: v 1  v 2  v 2  v 1  v 1,v 2,v 3  V: v 1  v 2  v 2  v 3  v 1  v 3

[ 10 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms What is this? The ”borders-with” relation (for the European mainland)

[ 11 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Graph Visualization The ”Graphviz” Tool: [ ] graph nfa { overlap=false; splines=true; DK -- DE; DE -- NL; DE -- BE; DE -- LU; DE -- FR; DE -- CH; DE -- AT; DE -- CZ; DE -- PL; … } graphviz ’.dot ’ file

[ 12 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Edge Information Often, information associated with edges e.g., Roadmaps: e.g., Powergrid: e.g., Bacon-index: … 305 km KBHAarhus 175 min 220 V Agave 70 Outlet Mr. & Mrs. Smith A.Jolie B.Pitt 10 A 2005

[ 13 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Graph Visualization (cont’d) The ”Graphviz” Tool: [ ] graph nfa { n n9 [label="Kirkevej"]; n9 -- n184 [label="Kongeledet"]; n7 -- n200 [label="Norasvej"]; n46 -- n10 [label="Norasvej"]; n7 -- n160 [label="Skovagervej"]; n28 -- n71 [label="Kirkevej"]; … } graphviz ’.dot ’ file

[ 14 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Outline Graphs Graph Representations Krak Data Representation Breadth First Search (BFS) Depth First Search (DFS) Topological Sorting Exercises Project (part 1): ”Visualization”

[ 15 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms 1) Edge List Representation Problem: We have to examine entire edge-list (in linear time) to answer questions about a node ’N’ Performance: boolean isEdge(Node n, Node m) O ( |E| ) List inEdges(Node n) O ( |E| ) List outEdges(Node n) O ( |E| ) E = [ (1,2), (2,1), (2,3), (3,1), (4,2) ] Just put all edges in ”one big” edge list: Space: O ( |E| )

[ 16 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms 2) Adjacency List Repr. Performance: boolean isEdge(Node n, Node m) O ( outdeg(n) ) List inEdges(Node n) O ( indeg(n) ) List outEdges(Node n) O ( outdeg(n) ) outgoing: 1: [ [ (1,2) ], 2: [ (2,1), (2,3) ], 3: [ (3,1) ], 4: [ (4,2) ] ] Each node has its own edge list of… incoming: 1: [ [ (2,1), (3,1) ], 2: [ (1,2), (4,2) ], 3: [ (2,3) ], 4: [ ] ] Space: O ( |V| + |E| ) outgoing… …and incoming edges:

[ 17 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms 3) Adjacency Matrix Repr. Performance: boolean isEdge(Node n, Node m) O ( 1 ) List inEdges(Node n) O ( |V| ) List outEdges(Node n) O ( |V| ) Space: O ( |V| 2 ) always! 

[ 18 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Outline Graphs Graph Representations Krak Data Representation Breadth First Search (BFS) Depth First Search (DFS) Topological Sorting Exercises Project (part 1): ”Visualization”

[ 19 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms ”KRAK” Data Representation Krak data: NB: only for teaching and research at ITU ! Size: ~ 150 MB (big) Sealand: roads Lots of challenges: Efficient visualization Effective browsing Efficient road searching Calculate fastest routes … ”KDV” (Kraks Danske Vejnet):

[ 20 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms UTM Coordinates UTM (Universal Transverse Mercator net): Approximated (x,y)- coordinates for earth 60x24 zones: (west) 01, …, 60 (east) (south) A, …, Z (north) Denmark is in zones: 32V + 32U + 33U KRAK: Coord’s converted to (”zone 32”, meters N of equator) E.g., US military 1940s ”Rued Langgaards Vej”: (x 1,y 1 ) = ( , ) (x 2,y 2 ) = ( , )

[ 21 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms ”KRAK” Data Representation Edges (roads): Vertices (intersections): , // from node# , // to node# , // length, meters , , 6, // road type 'Rued Langgaards Vej’, // name 0,0,0,0, // to/from house#s,,,,0,0, 2300,2300, // to/from zipcode 101,6005,0,,0,0, 10, // speed limit 1.375, // drive time, min,,, , 09/04/02, , // arc node# // (not used) , // node# , // national node# , // UTM: # meters // west of zone // UTM: # meters // north of equ’tr ”kdv_unload.txt””kdv_node_unload.txt”

[ 22 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms ”graphlib” Representation Node: Edge: package graphlib; public class Node { public final int index; public Node(int index) { this.index = index; } public final int getIndex() { return index; } package graphlib; public class Edge { // fields: public N v1; public N v2; protected byte direction; public static final byte FORWARD = 1, BACKWARD = 2, BOTH = FORWARD | BACKWARD; // constructors: protected Edge() {} public Edge(N n1, N n2, byte dir) {…} // methods: public N getStart() {…} public N getOtherEnd(N n) {…} public N getEnd(){…} } ”krak-kode-faap-1.zip”: Contains code for reading the KRAK data format and for building a graph!

[ 23 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms ”graphlib” Representation Graph: public class Graph, N extends Node> { // fields: public ArrayList nodes; //list of all nodes int edgeCount = 0; public ArrayList > edges; //  nodes: list of edges ArrayList > reverse_edges; //  nodes: list of rev-edges //(excepting the BOTH edges) // constructor: public Graph(ArrayList nodes) {…} // methods: public int getEdgeCount() {…} public void addEdges(Collection c) {…} public void addEdge(E e) {…} public N getNode(int index) {…} public Iterator outGoingEdges(Node n) {…} public Iterator incomingEdges(Node n) {…} } NB!: all lists start at index 1 (index 0 is bogus) NB!: ”node.index” are numbered & initialized 1..N+1

[ 24 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Outline Graphs Graph Representations Krak Data Representation Breadth First Search (BFS) Depth First Search (DFS) Topological Sorting Exercises Project (part 1): ”Visualization”

[ 25 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Breadth First Search (BFS) Breadth First Search (BFS): 1) Make queue of nodes to be visited 2) Enqueue start node in queue 3) While queue not empty do: 4) Dequeue node v from queue 5) If v not already visited then: 6) Mark v as visited (count++) 7) Enqueue all of v ’s neighbours

[ 26 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms BFS.java Set exploredNodes = new HashSet (); Queue queue = new LinkedList (); (1) queue.offer(start); (2) while (!queue.isEmpty()) { (3) Node v = queue.poll(); (4) if (!exploredNodes.contains(v)) { (5) System.out.print(v.index + " "); // print exploredNodes.add(v); (6) for (Edge edge: graph.edges.get(v.index)) {(7) Node w = edge.getOtherEnd(v); queue.offer(w); } dequeue 1) Make queue of nodes to be visited 2) Enqueue start node in queue 3) While queue not empty do: 4) Dequeue node v from queue 5) If v not already visited then: 6) Mark v as visited (count++) 7) Enqueue all of v ’s neighbours enqueue

[ 27 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Outline Graphs Graph Representations Krak Data Representation Breadth First Search (BFS) Depth First Search (DFS) Topological Sorting Exercises Project (part 1): ”Visualization”

[ 28 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Depth First Search (DFS) Depth First Search (DFS): 1) Make stack of nodes to be visited 2) Push start node onto stack 3) While stack not empty do: 4) Pop node v from stack 5) If v not already visited then: 6) Mark v as visited (count++) 7) Push all of v ’s neighbours onto stack

[ 29 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Exercise: DFS.java pop 1) Make stack of nodes to be visited 2) Push start node onto stack 3) While stack not empty do: 4) Pop node v from stack 5) If v not already visited then: 6) Mark v as visited (count++) 7) Push all of v ’s neighbours onto stack push

[ 30 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Outline Graphs Graph Representations Krak Data Representation Breadth First Search (BFS) Depth First Search (DFS) Topological Sorting Exercises Project (part 1): ”Visualization”

[ 31 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Topological Sorting (a DAG) (Assumption: no cycles) Uses: Sequence of activities in a building project Recalculation sequence in a spread-sheet Observation: In a finite acyclic graph there exists at least one node with indegree zero… “A topological ordering of a DAG is a linear ordering of its nodes in which each node comes before all nodes to which it has outbound edges.” -- Wikipedia

[ 32 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Topological Sorting (cont’d) Topological Sorting: Efficient implementation: Maintain ”HashMap ” of all indegrees Maintain ”Stack ” of nodes with indegree zero 1) Initialize empty topologically ordered node list 2) Repeat until no more nodes: 3) Pick a node v without incoming edges* 4) Push v onto topologically ordered list 5) Remove v from graph; and 6) Remove all edges out of v *) if no such node exists, the graph is cyclic Time: O ( |V| + |E| )

[ 33 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Outline Graphs Graph Representations Krak Data Representation Breadth First Search (BFS) Depth First Search (DFS) Topological Sorting Exercises Project (part 1): ”Visualization”

[ 34 ] Claus Brabrand, ITU, Denmark Mar 17, 2009Graphs & Graph Algorithms Exercises (week 8) 8.1: Breadth First Search Run the code (from lecture) on example graph in book 8.2: Breadth First Search w/ Levels Implement BFS, with explicit levels (distance from start) 8.3: Depth First Search Change the BFS to a DFS algorithm (try it on book ex.) 8.4: Indegrees Implement method ”int indegree(Node node)” in Graph 8.5: Topological Sorting Implement ”static ArrayList topsort(Graph )” [cf. Homepage]