abstract data types built on other ADTs Graphs abstract data types built on other ADTs
Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects with properties
Graph definitions(1) graph: set of vertices and set of edges connecting vertices: G(V,E) path: sequence of vertices connected by edges path length connected graph directed edge digraph directed path
Graph definitions(2) simple path cycle simple cycle Directed Acyclic Graph DAG edge weight (cost) path cost (weighted path length)
Graph definitions(3) complete graph complete digraph |E| = |V|2 dense digraph |E| = O(|V|2) sparse digraph |E| = O(|V|)
Graphs in computing (mainly) interested in sparse directed graphs for applications networks for communication transportation systems distributed computing java hierarchies – inheritance, instance, message
Example graph: mine tunnels DATA vertex: key id 3 coordinates edge: two vertex id’s
Graphs as Collections linear trees graphs
Graphs as Collections graph traversals no obvious order of traversal (like trees) no obvious starting point (no root) traversals may not reach every vertex by following edges (connectedness) traversals may return to a vertex (cycles)
Graph implementation(1) adjacency matrix – ideal for dense digraph n vertices, space: O(n2) Graph g A B char[] v A B C D boolean[][] e to D C f t t f t t f t from f t f t f f f f
Graph implementation(2) adjacency list – ideal for sparse digraph n vertices, k edges, space: O(n+k) Graph g 0 1 2 3 A B char[] v A B C D node[] e to D C 1 2 1 3 from 1 3
Sparse Directed Graph data structures Vertex { ID (key) information about vertex adjacency list of edges temporary data storage for algorithms } Edge { information about edge destination vertex temporary data storage for algorithms }
Operations on graphs collection class operations: paths and traversals access, insert, delete, update for vertices and edges edges are easy vertices may impact edges also paths and traversals path lengths weighted path lengths specialized algorithms e.g – path through mine
Operations on graphs e.g. delete edge algorithm: remove edge from adjacency list
Operations on graphs e.g. delete vertex delete edges to/from the vertex also algorithm: delete adjacency list of vertex search other adjacency lists and delete edges to this vertex delete vertex
Path algorithms shortest path (number of edges) shortest weighted path (more edges may be better) negative edge weights/costs
Example Graph in JAVA class Vertex { public String name; // Vertex name public LinkedList<Edge> adj; // edges from vertex public double dist; // Cost public Vertex prev; // Previous vertex on shortest path public int scratch; // Extra variable used in algorithm public Vertex( String nm ) { name = nm; adj = new LinkedList<Edge>( ); reset( ); } public void reset( ) // clears values used in algorithms { dist = Graph.INFINITY; prev = null; scratch = 0;
Example Graph in JAVA class Edge { public Vertex dest; // Second vertex in Edge public double cost; // Edge cost public double temp; // used in algorithms public Edge( Vertex d, double c ) dest = d; cost = c; }
Graph in JAVA Graph g vertexMap name A adj A dist dest prev X cost key/ vertex adj A dist dest prev X scratch cost temp
Graph in JAVA public class Graph { public static final double INFINITY = Double.MAX_VALUE; private HashMap<String,Vertex> vertexMap = new HashMap(); // maps String to Vertex public void addEdge( String sourceName, String destName, double cost ) Vertex v = getVertex( sourceName ); Vertex w = getVertex( destName ); v.adj.add( new Edge( w, cost ) ); }
Graph in JAVA private Vertex getVertex( String vertexName ) { Vertex v = (Vertex) vertexMap.get( vertexName ); if( v == null ) v = new Vertex( vertexName ); vertexMap.put( vertexName, v ); } return v;
Graph in JAVA A B 1 A C 1 D B 1 D A 1 B D 1 C B 1 A B A B C A B C D A sample file: A B 1 A C 1 D B 1 D A 1 B D 1 C B 1 A B C A B C D A B C D A B A B C D C D
Graph in JAVA Graph g vertexMap name A adj A dist dest prev X cost key/ vertex adj A dist dest prev X scratch cost temp