MAT 7003 : Mathematical Foundations (for Software Engineering) J Paul Gibson, A207 [email protected] http://www-public.it-sudparis.eu/~gibson/Teaching/MAT7003/ Graphs and Trees – Code to Detect Cycles http://www-public.it-sudparis.eu/~gibson/Teaching/MAT7003/L2-GraphsAndTrees-CyclesSolution.pdf 2012: J Paul Gibson T&MSP: Mathematical Foundations MAT7003/L2-GraphsAndTreesCyclesSolution.1 Programming Exercise: check for cycles? For any graph, such as G, node and arc labelled, with directed arcs, chose a textual (string) representation. Write a function/method/procedure (in the programming language of your choice) that reads the string representation and returns true if the given graph contains a cycle (loop) NOTE: Checking for cycles is very important because many graph algorithms work only if there are no cycles in the graph (otherwise they may get into an infinite loop) 2012: J Paul Gibson T&MSP: Mathematical Foundations MAT7003/L2-GraphsAndTreesCyclesSolution.2 Programming Exercise: check for cycles? Did Any Of You Re-use Code? Search on-line Library code Your own libraries/packages http://jgrapht.org/ /* ========================================== * JGraphT : a free Java graph-theory library * ========================================== * * Project Info: http://jgrapht.sourceforge.net/ * Project Creator: Barak Naveh (http://sourceforge.net/users/barak_naveh) * * (C) Copyright 2003-2008, by Barak Naveh and Contributors. */ 2012: J Paul Gibson T&MSP: Mathematical Foundations MAT7003/L2-GraphsAndTreesCyclesSolution.3 Programming Exercise: check for cycles? For all programming languages/IDEs you need to know how to use libraries properly : •Create your own library •Copy (or link to) someone elses library •Include libraries in a project There are often many different ways of doing this. You should know the advantages and disadvantages of each Here, I will download the zip and add it to my Java libraries 2012: J Paul Gibson T&MSP: Mathematical Foundations MAT7003/L2-GraphsAndTreesCyclesSolution.4 Programming Exercise: check for cycles? 2012: J Paul Gibson T&MSP: Mathematical Foundations MAT7003/L2-GraphsAndTreesCyclesSolution.5 Programming Exercise: check for cycles? Now I Can Write My Test Code 2012: J Paul Gibson T&MSP: Mathematical Foundations MAT7003/L2-GraphsAndTreesCyclesSolution.6 Programming Exercise: check for cycles? Download the code from the website and load into Eclipse Take note of: •Original Code Source is properly referenced in documentaion/comments •Use of Javadoc tool to generate documentation 2012: J Paul Gibson T&MSP: Mathematical Foundations MAT7003/L2-GraphsAndTreesCyclesSolution.7 Programming Exercise: check for cycles? Execute the test code 2012: J Paul Gibson T&MSP: Mathematical Foundations MAT7003/L2-GraphsAndTreesCyclesSolution.8 Programming Exercise: check for cycles? public static void testNoCycleGraph(){ DefaultDirectedGraph<String, DefaultEdge> graphNoCycle = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class); String vertexA = "A"; String vertexB = "B"; String vertexC = "C"; graphNoCycle.addVertex(vertexA); graphNoCycle.addVertex(vertexB); graphNoCycle.addVertex(vertexC); Quick Look At Code graphNoCycle.addEdge(vertexA, vertexB); graphNoCycle.addEdge(vertexB, vertexC); System.out.println(graphNoCycle); CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<String, DefaultEdge>(graphNoCycle); Set<String> cycleVertices = cycleDetector.findCycles(); System.out.println("Cycle vertices are "+ cycleVertices); boolean foundCycleFromA = cycleDetector.detectCyclesContainingVertex(vertexA); boolean foundCycleFromB = cycleDetector.detectCyclesContainingVertex(vertexB); System.out.println("Found cycle including A is "+ foundCycleFromA); System.out.println("Found cycle including B is "+ foundCycleFromB); } 2012: J Paul Gibson T&MSP: Mathematical Foundations MAT7003/L2-GraphsAndTreesCyclesSolution.9
© Copyright 2026 Paperzz