Upload
briana-adams
View
222
Download
0
Embed Size (px)
Citation preview
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
1. no obvious order of traversal (like trees)
2. no obvious starting point (no root)
3. traversals may not reach every vertex by following edges (connectedness)
4. traversals may return to a vertex (cycles)
Graph implementation(1)
• adjacency matrix – ideal for dense digraph
• n vertices, space: O(n2)
A B
D C
Graph g
char[] v
boolean[][] eA B C D
f t t f
t t f t
f t f t
f f f f
from
to
Graph implementation(2)
• adjacency list – ideal for sparse digraph
• n vertices, k edges, space: O(n+k)
A B
D C
Graph g
char[] v
node[] eA B C D
1
from
to
2
0 3
1 3
1
0 1 2 3
Sparse Directed Graphdata 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
1. collection class operations:– access, insert, delete, update for vertices and edges
• edges are easy• vertices may impact edges also
2. paths and traversals– path lengths– weighted path lengths
3. specialized algorithms– e.g – path through mine
Operations on graphs
• e.g. delete edge
algorithm:
1. remove edge from adjacency list
Operations on graphs
• e.g. delete vertex delete edges to/from the vertex also
algorithm:
1. delete adjacency list of vertex
2. search other adjacency lists and delete edges to this vertex
3. delete vertex
Path algorithms
• shortest path (number of edges)
• shortest weighted path (more edges may be better)
• negative edge weights/costs
Example Graph in JAVAclass 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
key/ vertex
A
name A
adj
dist
prevscratch
dest
costX
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; }
D
Graph in JAVA
sample file:A B 1A C 1D B 1D A 1B D 1C B 1
A B
A B
CA B
C
DC
A B
DC
A B
DC
A B
Graph in JAVA
Graph g
vertexMap
key/ vertex
A
name A
adj
dist
prevscratch
dest
costX
temp