claus brabrand, itu, denmark mar 09, 2010graphs & graph algorithms førsteårsprojekt (f2010)...

38
Claus Brabrand, ITU, Denmark Mar 09, 2010 Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ [email protected] ] IT University of Copenhagen

Post on 21-Dec-2015

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms

Førsteårsprojekt(F2010)

Claus Brabrand[ [email protected] ]

IT University of Copenhagen

Page 2: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms

Graphs &Graph Algorithms

Claus Brabrand[ [email protected] ]

IT University of Copenhagen

Page 3: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 3 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Page 4: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 4 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Outline

Graphs Graph Representations Breadth First Search (BFS) Depth First Search (DFS) Topological Sorting Exercises Krak Data Representation Project (part 1): ”Visualization”

Page 5: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 5 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Definition: GRAPH

A ”graph G = (V,E)” consists of: V: a set of vertices (nodes) (knuder) E: a set of edges (arcs) (kanter)

where E V V

There is an edge between ’v1’ and ’v2’ if (v1, v2) E (abbreviated: ”v1

v2”)

Example:

V = { 1, 2, 3, 4 }

E = { (1,2), (2,1), (2,3), (3,1), (4,2) }vertex

edge

Page 6: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 6 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Directed vs. Undirected

Directed graph: (orienteret graf)

Undirected graph: (ikke-orienteret)

V = { 1, 2, 3, 4 }

E = { (1,2), (2,1), (2,3), (3,1), (4,2) }

V = { 1, 2, 3, 4 }

E = { (1,2), (2,1), (1,3), (3,1), (2,3), (3,2), (2,4), (4,2) }

v1 v2 v2 v1

Page 7: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 7 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Graph Examples

Roadmap: roads and intersections project! Powergrid: wires and connexions Rivers: river and confluences World Wide Web: pages and links Facebook: people and friends-of Data structures: objects and references Class hierarchies: classes and inheritances Bacon index: people and co-starring

Page 8: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 8 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Graph Terminology

A path (sti) is a list of adjacent edges: p = [e0, e1, …, en]

A cycle (kreds) is a non-empty path from a vertex to itself:

p = [e0, e1, …, en]

v0 v1 … vn+1e0 e1 en

v0 v1 … v0

(v0,_) (_,v0)

= =

(v0,v1) (vn,vn+1)

= =

(v1,v2)=

Page 9: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 9 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Graph Terminology (cont’d)

A directed acyclic graph (DAG) (orienteret acyklisk graf) is a connected graph w/o cycles:

A tree (træ) is a connected acyclicgraph every node has indegree 0 or 1 (i.e., a DAG w/o ”sharing”):

Page 10: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 10 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Graph Properties

A particular graph ”G = (V,E)” is said to be:

Reflexive:

Symmetric:

Transitive:

v1V: v1 v1

v1,v2V: v1 v2 v2 v1

v1,v2,v3V: v1 v2 v2 v3 v1 v3

Page 11: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 11 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Which properties does the graph satisfy?A. reflexive

B. symmetric

C. transitive

D. reflexive, symmetric, and transitive

E. none of the above answers are correct

A

C

E

a b c d

Page 12: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 12 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Which properties does the graph satisfy?A. reflexive

B. symmetric

C. transitive

D. reflexive, symmetric, and transitive

E. none of the above answers are correct

A

C

E

a b c d

Page 13: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 13 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

What is this?

The”borders-with”

relation(for the

Europeanmainland)

Page 14: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 14 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Graph Visualization

The ”Graphviz” Tool: [ http://www.graphviz.org ]

graph nfa { overlap=false; splines=true;

DK -- DE; DE -- NL; DE -- BE; DE -- LU; DE -- FR; DE -- CH; DE -- AT; DE -- CZ; DE -- PL; …}

graphviz

’.dot’ file

Page 15: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 15 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Edge Information

Often, information associated with edges

e.g., Roadmaps:

e.g., Powergrid:

e.g., Bacon-index:

305 kmKBHAarhus

220 V Agave70Outlet

Mr. & Mrs. SmithA.JolieB.Pitt

Page 16: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 16 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Graph Visualization (cont’d)

The ”Graphviz” Tool: [ http://www.graphviz.org ]

graph nfa { n571 -- n9 [label="Kirkevej"]; n9 -- n184 [label="Kongeledet"]; n7 -- n200 [label="Norasvej"]; n46 -- n10 [label="Norasvej"]; n7 -- n160 [label="Skovagervej"]; n28 -- n71 [label="Kirkevej"]; …}

graphviz

’.dot’ file

Page 17: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 17 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Outline

Graphs Graph Representations Breadth First Search (BFS) Depth First Search (DFS) Topological Sorting Exercises Krak Data Representation Project (part 1): ”Visualization”

Page 18: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 18 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

"Edge List" Representation

Performance: boolean isEdge(Node n, Node m)? void addEdge(Node n, Node m)? void deleteEdge(Node n, Node m)? List<Node> getEdges(Node n)?

E = [ (1,2), (2,1), (2,3), (3,1), (4,2) ]

Just put all edges in ”one big” edge list:

A) O(1)B) O(log(|E|))C) O(|E|)D) O(|E|*log(|E|))E) O(|E|2)F) Something else

O(|E|)O(1)O(|E|)O(|E|)

Page 19: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 19 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

"Adjacency Matrix" Repr.

Performance: boolean isEdge(Node n, Node m)? void addEdge(Node n, Node m)? void deleteEdge(Node n, Node m)? List<Node> getEdges(Node n)?

A) O(1)B) O(log(|V|))C) O(|V|)D) O(|V|*log(|V|))E) O(|V|2)F) Something else

Make a matrix with entry M[i,j]=1 iff there's an edge from vertex #i to vertex #j

O(1)O(1)O(1)O(|V|)

Page 20: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 20 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

What about Space?

Edge List:

Adjacency Matrix:

O(|V|2)

O(|E|)

E = [ (1,2), (2,1), (2,3), (3,1), (4,2) ]

always! even for sparse graphs!

Page 21: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 21 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

"Adjacency List" Repr.

Performance: boolean isEdge(Node n, Node m)? void addEdge(Node n, Node m)? void deleteEdge(Node n, Node m)? List<Node> getEdges(Node n)?

outgoing:1: [ [ (1,2) ],2: [ (2,1), (2,3) ],3: [ (3,1) ],4: [ (4,2) ] ]

Each node has its own edge list of…

incoming:1: [ [ (2,1), (3,1) ],2: [ (1,2), (4,2) ],3: [ (2,3) ],4: [ ] ]

outgoing… …and incoming edges:

O( outdeg(n) )

O( 1 )

O( in(n)+out(n) )

O( in(n)+out(n) )

Page 22: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 22 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

What about Space? (cont'd)

Edge List:

Adjacency Matrix:

Adjacency List:

O(|V|2)

O(|V|+|E|)

O(|E|)

E = [ (1,2), (2,1), (2,3), (3,1), (4,2) ]

outgoing:1: [ [ (1,2) ],2: [ (2,1), (2,3) ],3: [ (3,1) ],4: [ (4,2) ] ]

incoming:1: [ [ (2,1), (3,1) ],2: [ (1,2), (4,2) ],3: [ (2,3) ],4: [ ] ]

always!

Page 23: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 23 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Outline

Graphs Graph Representations Breadth First Search (BFS) Depth First Search (DFS) Topological Sorting Exercises Krak Data Representation Project (part 1): ”Visualization”

Page 24: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 24 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Breadth First Search (BFS)

Breadth First Search (BFS):1) Make queue of nodes to be visited2) Enqueue start node in queue3) While queue not empty do:4) Dequeue node v from queue5) Mark v as visited (count++)6) Enqueue all of v’s unvisited neighbours

Page 25: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 25 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

BFS.java

Set<Node> exploredNodes = new HashSet<Node>();Queue<Node> queue = new LinkedList<Node>(); (1)

queue.offer(start); (2)while (!queue.isEmpty()) { (3) Node v = queue.poll(); (4) System.out.print(v.index + " "); // print visited node exploredNodes.add(v); (5) for (Edge<Node> edge: graph.getEdges(v)) { (6) Node w = edge.getOtherEnd(v); // get v's neighbour w if (!exploredNodes.contains(w)) { // if w unvisited queue.offer(w); } }}

dequeue

1) Make queue of nodes to be visited2) Enqueue start node in queue3) While queue not empty do:4) Dequeue node v from queue5) Mark v as visited (count++)6) Enqueue all of v’s unvisited neighbours

enqueue

enqueue

Page 26: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 26 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Outline

Graphs Graph Representations Breadth First Search (BFS) Depth First Search (DFS) Topological Sorting Exercises Krak Data Representation Project (part 1): ”Visualization”

Page 27: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 27 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Depth First Search (DFS)

Depth First Search (DFS):1) Make stack of nodes to be visited2) Push start node onto stack3) While stack not empty do:4) Pop node v from stack5) Mark v as visited (count++)6) Push all of v’s unvisited neighbours onto stack

Page 28: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 28 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Exercise: DFS.java

pop

1) Make stack of nodes to be visited2) Push start node onto stack3) While stack not empty do:4) Pop node v from stack5) Mark v as visited (count++)6) Push all of v’s unvisited neighbours onto stack

push

push

Page 29: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 29 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Outline

Graphs Graph Representations Breadth First Search (BFS) Depth First Search (DFS) Topological Sorting Exercises Krak Data Representation Project (part 1): ”Visualization”

Page 30: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 30 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Topological Sorting (a DAG) (Assumption: no cycles)

Uses: Sequence of activities in a building project Recalculation sequence in a spread-sheet

Observation: In a finite acyclic graph there exists at least one node

with indegree zero…

“A topological ordering of a DAG is a linear ordering of its nodes in which each node comes before all nodes to which it has outbound edges.” -- Wikipedia

Page 31: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 31 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Topological Sorting (cont’d)

Topological Sorting:

Efficient implementation: Maintain ”HashMap<Node,Integer>” of all indegrees Maintain ”Stack<Node>” of nodes with indegree zero

1) Initialize empty topologically ordered node list2) Repeat until no more nodes:3) Pick a node v without incoming edges4) Push v onto topologically ordered list5) Remove v from graph; and6) Remove all edges out of v

*) if no such node exists, the graph is cyclic

Time: O( |V| + |E| )

*

Page 32: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 32 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Outline

Graphs Graph Representations Breadth First Search (BFS) Depth First Search (DFS) Topological Sorting Exercises Krak Data Representation Project (part 1): ”Visualization”

Page 33: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 33 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

Exercises

1) Graph: Make a graph interface and implementation

2) Breadth First Search: Run the code (from this lecture) on example graph

3) Depth First Search: Change the BFS to a DFS algorithm (and try it out)

4) Draw graph (using GraphViz): Add a "toDot()" method to your Graph (and try it out)

5) Topological Sorting: Implement "List<Node> topSort(Graph<...>)"

Page 34: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 34 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

”KRAK” Data Representation

Krak data: NB: only for teaching

and research at ITU !

Size: ~ 150 MB (big) Sealand: 209.402 roads

Lots of challenges: Efficient visualization Effective browsing Efficient road searching Calculate fastest routes …

”KDV” (Kraks Danske Vejnet):

Page 35: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 35 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

UTM Coordinates

UTM (Universal Transverse Mercator net): Approximated (x,y)-

coordinates for earth 60x24 zones:

(west) 01, …, 60 (east) (south) A, …, Z (north)

Denmark is in zones: 32V + 32U + 33U

KRAK: Coord’s converted to (”zone 32”, meters N of equator)

E.g.,

US military 1940s

”Rued Langgaards Vej”:(x1,y1) = (725696.40753, 6174169.66806)(x2,y2) = (725860.58568, 6174110.34971)

Page 36: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 36 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

”KRAK” Data Representation

Edges (roads): Vertices (intersections):132371, // from node#132196, // to node#199.34533, // length, meters208484,208484,6, // road type'Rued Langgaards Vej’, // name0,0,0,0, // to/from house#s,,,,0,0, 2300,2300, // to/from zipcode101,6005,0,,0,0,10, // speed limit1.375, // drive time, min,,,10178744,09/04/02,4164866

208484, // arc node# // (not used)

132371, // node#

441762, // national node#

725696.40753, // UTM: # meters // west of zone 28

6174169.66806 // UTM: # meters // north of equ’tr

”kdv_unload.txt” ”kdv_node_unload.txt”

Page 37: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 37 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

”graphlib” Representation

Node:

Edge:

package graphlib;

public class Node { public final int index; public Node(int index) { this.index = index; }

public final int getIndex() { return index; }}

package graphlib;

public class Edge<N extends Node> { // fields: public N v1; public N v2; protected byte direction; public static final byte FORWARD = 1, BACKWARD = 2, BOTH = FORWARD | BACKWARD;

// constructors: protected Edge() {…} public Edge(N n1, N n2, byte dir) {…}

// methods: public N getStart() {…} public N getOtherEnd(N n) {…} public N getEnd(){…}}

”krak-kode-faap-1.zip”:Contains code for reading the KRAK data format and for building a graph!

Page 38: Claus Brabrand, ITU, Denmark Mar 09, 2010Graphs & Graph Algorithms Førsteårsprojekt (F2010) Claus Brabrand [ brabrand@itu.dk ] IT University of Copenhagen

[ 38 ]Claus Brabrand, ITU, Denmark Graphs & Graph Algorithms Mar 09, 2010

”graphlib” Representation

Graph:public class Graph<E extends Edge<N>, N extends Node> { // fields: public ArrayList<N> nodes; //list of all nodes int edgeCount = 0; public ArrayList<ArrayList<E>> edges; //nodes: list of edges ArrayList<ArrayList<E>> reverse_edges; //nodes: list of rev-edges //(excepting the BOTH edges) // constructor: public Graph(ArrayList<N> nodes) {…}

// methods: public int getEdgeCount() {…} public void addEdges(Collection<E> c) {…} public void addEdge(E e) {…} public N getNode(int index) {…} public Iterator<E> outGoingEdges(Node n) {…} public Iterator<E> incomingEdges(Node n) {…}}

NB!: all lists start at index 1 ( index 0 is bogus :-/ )NB!: ”node.index” are numbered & initialized 1..N+1