Download presentation
Presentation is loading. Please wait.
1
abstract data types built on other ADTs
Graphs abstract data types built on other ADTs
2
Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects with properties
3
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
4
Graph definitions(2) simple path cycle simple cycle
Directed Acyclic Graph DAG edge weight (cost) path cost (weighted path length)
5
Graph definitions(3) complete graph complete digraph |E| = |V|2
dense digraph |E| = O(|V|2) sparse digraph |E| = O(|V|)
6
Graphs in computing (mainly) interested in sparse directed graphs for applications networks for communication transportation systems distributed computing java hierarchies – inheritance, instance, message
7
Example graph: mine tunnels
DATA vertex: key id 3 coordinates edge: two vertex id’s
8
Graphs as Collections linear trees graphs
9
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)
10
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
11
Graph implementation(2)
adjacency list – ideal for sparse digraph n vertices, k edges, space: O(n+k) Graph g A B char[] v A B C D node[] e to D C 1 2 1 3 from 1 3
12
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 }
13
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
14
Operations on graphs e.g. delete edge algorithm:
remove edge from adjacency list
15
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
16
Path algorithms shortest path (number of edges)
shortest weighted path (more edges may be better) negative edge weights/costs
17
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;
18
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; }
19
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
20
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 ) ); }
21
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;
22
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
23
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.