Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Graphs - Object Oriented Programming and Data Structures - Lecture Slides, Slides of Object Oriented Programming

Lecture from Object Oriented Programming and Data Structures course with following key points: Graphs, Representation, Adjacency Matrix, Shortest Paths in Graphs, Dijkstras Shortest Path Algorithm, Execution Time, Special Case, Matrix Representation

Typology: Slides

2013/2014

Uploaded on 01/29/2014

sundar
sundar 🇮🇳

4.7

(10)

112 documents

1 / 44

Toggle sidebar

Related documents


Partial preview of the text

Download Graphs - Object Oriented Programming and Data Structures - Lecture Slides and more Slides Object Oriented Programming in PDF only on Docsity! Graphs docsity.com Time to do A4, Recursion Histogram: max: 28.45 [00:02): 17 av: 5.2 [02:04): 102 median: 4.5 [04:06): 134 [06:08): 50 [08:10): 29 [10:12): 8 [12:14): 6 [14:16): 2 [16:18): 1 [18:30): 5 If you took more than 6-7 hours for this assignment, you may have been wasting your time. A certain amount of “floundering”, just trying things, is good. But after a point, it just wastes time. Seek help if after an hour on one of the recursion problems you are stuck. docsity.com Applications of Graphs  Communication networks  The internet is a huge graph  Routing and shortest path problems  Commodity distribution (flow)  Traffic control  Resource allocation  Geometric modeling  ... docsity.com Graph Definitions  A directed graph (or digraph) is a pair (V, E) where  V is a set  E is a set of ordered pairs (u,v) where u,v V  Sometimes require u  v (i.e. no self-loops)  An element of V is called a vertex (pl. vertices) or node  An element of E is called an edge or arc  |V| is the size of V, often denoted by n  |E| is size of E, often denoted by m docsity.com Example Directed Graph (Digraph) V = {a,b,c,d,e,f } E = {(a,b), (a,c), (a,e), (b,c), (b,d), (b,e), (c,d), (c,f), (d,e), (d,f), (e,f)} |V| = 6, |E| = 11 b a c d e f docsity.com More Graph Terminology  path: sequence of adjacent vertexes  length of path: number of edges  simple path: no vertex is repeated simple path of length 2: (b, c, d) simple path of length 0: (b) not a simple path: (b, c, e, b, c, d) b c d e docsity.com More Graph Terminology  cycle: path that ends at its beginning  simple cycle: only repeated vertex is its beginning/end  acyclic graph: graph with no cycles  dag: directed acyclic graph b c d e cycles: (b, c, e, b) (b, c, e, b, c, e, b) simple cycle: (c, e, b, c) graph shown is not a dag Question: is (d) a cycle? No. A cycle must have at least one edge docsity.com Is this a dag?  Intuition: A dag has a vertex with indegree 0. Why?  This idea leads to an algorithm: A digraph is a dag if and only if one can iteratively delete indegree-0 vertices until the graph disappears b a c d e f docsity.com Is this a dag?  Intuition: A dag has a vertex with indegree 0. Why?  This idea leads to an algorithm: A digraph is a dag if and only if one can iteratively delete indegree-0 vertices until the graph disappears c d e f docsity.com Is this a dag?  Intuition: A dag has a vertex with indegree 0. Why?  This idea leads to an algorithm: A digraph is a dag if and only if one can iteratively delete indegree-0 vertices until the graph disappears d e f docsity.com Is this a dag?  Intuition: A dag has a vertex with indegree 0. Why?  This idea leads to an algorithm: A digraph is a dag if and only if one can iteratively delete indegree-0 vertices until the graph disappears e f docsity.com Coloring of an undirected graph: an assignment of a color to each node such that no two adjacent vertices get the same color How many colors are needed to color this graph? Graph Coloring b a c d e f docsity.com A coloring of an undirected graph: an assignment of a color to each node such that no two adjacent vertices get the same color How many colors are needed to color this graph? Graph Coloring b a c d e f 3 docsity.com An Application of Coloring  Vertices are jobs  Edge (u,v) is present if jobs u and v each require access to the same shared resource, so they cannot execute simultaneously  Colors are time slots to schedule the jobs  Minimum number of colors needed to color the graph = minimum number of time slots required b a c d e f docsity.com Detecting Planarity Kuratowski's Theorem A graph is planar if and only if it does not contain a copy of K5 or K3,3 (possibly with other nodes along the edges shown) K3,3K5 docsity.com Detecting Planarity Early 1970’s John Hopcroft spent time at Stanford, talked to grad student Bob Tarjan (now at Princeton). Together, they developed a linear-time algorithm to test a graph for planarity. Significant achievement. Won Turing Award docsity.com The Four-Color Theorem Every planar graph is 4-colorable (Appel & Haken, 1976) Interesting history. “Proved” in about 1876 and published, but ten years later, a mistake was found. It took 90 more years for a proof to be found. Countries are nodes; edge between them if they have a common boundary. You need 5 colors to color a map —water has to be blue! docsity.com The following are equivalent  G is bipartite  G is 2-colorable  G has no cycles of odd length Bipartite Graphs docsity.com Traveling Salesperson Find a path of minimum distance that visits every city Amsterdam Rome Boston Atlanta London Paris Copenhagen Munich Ithaca New York Washington 1202 1380 1214 1322 1356 1002 512 216 441 189 160 15561323 419 210 224 132 660 505 1078 docsity.com Representations of Graphs 2 4 Adjacency List 1 2 3 4 1 0 1 0 1 2 0 0 1 0 3 0 0 0 0 4 0 1 1 0 Adjacency Matrix 1 2 34 3 2 1 4 3 2 3 docsity.com Depth-First Search • Follow edges depth-first starting from an arbitrary vertex r, using a stack to remember where you came from • When you encounter a vertex previously visited, or there are no outgoing edges, retreat and try another path • Eventually visit all vertices reachable from r • If there are still unvisited vertices, repeat • O(m) time Difficult to understand! Let’s write a recursive procedure docsity.com Depth-First Search boolean[] visited; node u is visited means: visited[u] is true To visit u means to: set visited[u] to true Node u is REACHABLE from node v if there is a path (u, …, v) in which all nodes of the path are unvisited. 4 1 0 5 2 3 6 Suppose all nodes are unvisited. The nodes that are REACHABLE from node 1 are 1, 0, 2, 3, 5 The nodes that are REACHABLE from 4 are 4, 5, 6. docsity.com Depth-First Search boolean[] visited; To “visit” a node u: set visited[u] to true. Node u is REACHABLE from node v if there is a path (u, …, v) in which all nodes of the path are unvisited. 4 1 0 5 2 3 6 Suppose 2 is already visited, others unvisited. The nodes that are REACHABLE from node 1 are 1, 0, 5 The nodes that are REACHABLE from 4 are 4, 5, 6. docsity.com Depth-First Search /** Node u is unvisited. Visit all nodes that are REACHABLE from u. */ public static void dfs(int u) { } Let u be 1 The nodes to be visited are 0, 2, 3, 5 4 1 0 5 2 3 6 visited[u]= true; for each edge (u, v) if v is unvisited then dfs(v); Suppose the for each loop visits neighbors in numerical order. Then dfs(1) visits the nodes in this order: 1, 0, 2, 3, 5 docsity.com Depth-First Search /** Node u is unvisited. Visit all nodes that are REACHABLE from u. */ public static void dfs(int u) { } visited[u]= true; for each edge (u, v) if v is unvisited then dfs(v); Example: There may be a different way (other than array visited) to know whether a node has been visited That’s all there is to the basic dfs. You may have to change it to fit a particular situation. Example: Instead of using recursion, use a loop and maintain the stack yourself. docsity.com Depth-First Search /** Node u is unvisited. Visit all nodes that are REACHABLE from u. */ public static void dfs(int u) { } visited[u]= true; for each edge (u, v) if v is unvisited then dfs(v); That’s all there is to the basic dfs. You may have to change it to fit a particular situation. Example: In Bfly, there is no need for a parameter, because the current position of the Bfly takes the place of u. But then the specification must be changed, and probably the Bfly should be back at its original tile after each iteration of the loop. Make sure that is in the specification! docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved