Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both...
-
Upload
briana-adams -
Category
Documents
-
view
220 -
download
0
Transcript of Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both...
![Page 1: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/1.jpg)
Graphs
abstract data types built on other ADTs
![Page 2: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/2.jpg)
Graphs in computing
• typically interested in both vertices and edges as objects (e.g., networks)
objects with properties
![Page 3: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/3.jpg)
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
![Page 4: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/4.jpg)
Graph definitions(2)
• simple path
• cycle
• simple cycle
• directed acyclic graph DAG
• edge weight (cost)
• path cost (weighted path length)
![Page 5: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/5.jpg)
Graph definitions(3)
• complete graph
• complete digraph |E| = |V|2
• dense digraph |E| = O(|V|2)
• sparse digraph |E| = O(|V|)
![Page 6: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/6.jpg)
Graphs in computing
• (mainly) interested in sparse directed graphs for applications– networks for communication– transportation systems– distributed computing– java hierarchies – inheritance, instance,
message
![Page 7: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/7.jpg)
Example graph: mine tunnels
DATA
vertex:
key id
3 coordinates
edge:
two vertex id’s
![Page 8: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/8.jpg)
Graphs as Collections
• linear
• trees
• graphs
![Page 9: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/9.jpg)
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)
![Page 10: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/10.jpg)
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
![Page 11: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/11.jpg)
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
![Page 12: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/12.jpg)
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
}
![Page 13: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/13.jpg)
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
![Page 14: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/14.jpg)
Operations on graphs
• e.g. delete edge
algorithm:
1. remove edge from adjacency list
![Page 15: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/15.jpg)
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
![Page 16: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/16.jpg)
Path algorithms
• shortest path (number of edges)
• shortest weighted path (more edges may be better)
• negative edge weights/costs
![Page 17: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/17.jpg)
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; } }
![Page 18: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/18.jpg)
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; }}
![Page 19: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/19.jpg)
Graph in JAVA
Graph g
vertexMap
key/ vertex
A
name A
adj
dist
prevscratch
dest
costX
temp
![Page 20: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/20.jpg)
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 ) ); }
![Page 21: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/21.jpg)
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; }
![Page 22: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/22.jpg)
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
![Page 23: Graphs abstract data types built on other ADTs. Graphs in computing typically interested in both vertices and edges as objects (e.g., networks) objects.](https://reader036.fdocuments.us/reader036/viewer/2022062409/56649ec75503460f94bd2ea6/html5/thumbnails/23.jpg)
Graph in JAVA
Graph g
vertexMap
key/ vertex
A
name A
adj
dist
prevscratch
dest
costX
temp