23
Graphs 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

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Graph in JAVA

Graph g

vertexMap

key/ vertex

A

name A

adj

dist

prevscratch

dest

costX

temp