36
Graphs Ric Glassey [email protected]

Graphs - Skolan för datavetenskap och kommunikation | KTH · • Graphs • Aim: “Introduce ... Implementation of Graph API ... • We could choose from an array-based implementation

Embed Size (px)

Citation preview

Overview• Graphs

• Aim: “Introduce graphs as a fundamental data structure that can be applied in many real-world problems”

• Applications of graphs

• Definition and types of graphs

• Underlying representation & performance

Applications of Graphs

Applications of Graphs

Application Item Connection

Computer Network Node Network Link

Web Page Link

Software Method Call

Social Network Person Friendship

T-bana Map Stop Rail

Modelling with Graphs

• Many problems can be tackled with graphs

• Scale and structure of graph can range massively

• Operations on items and connections are the same irrespective of scale/structure

• Challenge is to find the best representation to fit the application requirements

Definitions

Graph

• A graph is a set of vertices and a collection of edges that each connect a pair of vertices

• V is the unique set of vertices

• E is the collection of edges

• v-w represents the connection between vertex v and vertex w

Graph

0 4

5

1

2

3

6

Vertex

Value

Edge

V = [0, 1, 2, 3, 4, 5, 6]

E = [ 0-0, 0-4, 1-2, 1-5, 2-1, 3-4, 4-0, 4-3, 4-6, 5-1, 5-4, 5-6, 6-4 6-5]

Anomalies• We can find self-loops and parallel edges

• Applications may need these, but care is required when processing

0 1

Parallel EdgesSelf-loop

Terminology• Adjacent: One V connected to another V

• Degree: The number of E connected to any V

• Subgraph: A subset of a larger graph

0 4

5

1

6

V(4) has degree '3'

V(1) is adjacent to V(5)

V(4,5,6) is a subgraph

Terminology• Path: sequence of V connected by E

• Simple Path: no repeated visits to any V

• Cycle: A path that starts and ends at one V

0 4

5

1

2

6

Terminology

• A graph is Connected if there is a path from every V to any other V

• A graph is Acyclic if there are no cycles

• A graph is a Tree when it is both connected and acyclic

Terminology• The Density of a graph is the proportion of all possible pairs of V connected by E

• A graph is Sparse if there are relatively few E present

• A graph is Dense graph has relatively few edges missing

V=50, E=200 V=50, E=1000

Types of Graphs

Types• Undirected Graphs (graphs)

• Simple connection between V and E

• Directed Graphs (digraphs) *

• Direction of connection is significant between V

• Edge-weighted Graphs *

• Each connection E has an associated weight

• Edge-weighted Digraphs *

• Direction and weight on connections

Types - Graph, Digraph

0 4

5

1

2

3

6 0 4

5

1

2

3

6

Types - Edge-weighted

0 4

5

1

2

3

62

5

5

6 2

7

8

0 4

5

1

2

3

62

5

5

6 2

7

8

Graph API

Public class Graph

Graph(int V) Create a V vertex graph with no edges

Graph(InputStream in) Create a graph from input stream (e.g. from File)

int V( ) Number of V

int E( ) Number of E

void addEdge(int v, int w) Add edge v-w to this graph

Iterable<Integer> adjacent(int v) Return collection of V adjacent to v

String toString( ) Return a string representation of graph

public static int degree(Graph G, int v) {int degree = 0;for (int w : G.adjacent(v)) {

degree++;}return degree;

}

public static int maxDegree(Graph G) { int max = 0;

for (int v = 0; v < G.V(); v++){ if (degree(G, v) > max) { max = degree(G, v); }

}return max;

}

public static int avgDegree(Graph G){return 2 * G.E() / G.V();

}

Underlying Representation

Implementation of Graph API

• Recall List data structure

• API: Add(), Next(), Remove() etc

• We could choose from an array-based implementation or a linked list of objects

• Each represents an underlying representation

• Each had differing performance implications

Data Structure

Operation

Array Linked List

Search O(1) O(n)

Insert O(n) O(1)

Delete O(n) O(1)

Implementation of Graph API

• Graph API has two main underlying representations

• Adjacency Matrix

• Adjacency List

• Both focus on modelling the adjacency of vertices, but each has implications on space/time complexity

Adjacency Matrix

01234560|10001001|00100102|01000003|00001004|10010115|01001016|0000110

0 4

5

1

2

3

6

Adjacency Matrix• V by V array

• A True (1) entry in table indicates there is an edge between v and w

01234560|10001001|00100102|01000003|00001004|10010115|01001016|0000110

0 4

5

1

2

3

6

Adjacency Matrix

• Advantages:

• Finding if vertices are adjacent is fast

• More appropriate for dense graphs

• Disadvantages

• Add/Remove potentially affects many cells

• Less appropriate for sparse graphs

Adjacency Matrix

Adjacency Matrix

A lot of wasted space

01234560|10001001|00100102|01000003|00001004|10010115|01001016|0000110

Adjacency List

0 4

5

1

2

3

6

Adj [ ]

0

1

2

3

4

5

6

0 4

2 5

1

4

0 3 5 6

1 4 6

4 5

Adjacency List

0 4

5

1

2

3

6

Adj [ ]

0

1

2

3

4

5

6

0 4

2 5

1

4

0 3 5 6

1 4 6

4 5

?

Adjacency List

• Advantages

• Space efficient representation

• Most operations are faster than matrix

• Disadvantages

• Edge removal and adjacency testing are less efficient

Performance Comparison

Data Structure

Operation

Adjacency Matrix

Adjacency List

Storage O(V2) O(V+E)Add Vertex O(V2) O(1)Add Edge O(1) O(1)Remove Vertex O(V2) O(E)Remove Edge O(1) O(E)Are Adjacent O(1) O(V)

Adj [ ]

0

1

2

3

4

5

6

0 4

2 5

1

4

0 3 5 6

1 4 6

4 5

01234560|10001001|00100102|01000003|00001004|10010115|01001016|0000110

Readings• Algorithms and Data Structures *required reading*

• Nilsson

• Graphs

• http://www.nada.kth.se/~snilsson/algoritmer/grafer/

• Introduction to Algorithms, 3rd Edition

• Corman et al

• Chapter 22, Elementary Graph Algorithms

• http://kth-primo.hosted.exlibrisgroup.com/KTH:KTH_SFX2560000000068328

• Algorithms 4th Edition

• Sedgwick & Wayne

• Chapter 4, Graphs

• http://kth-primo.hosted.exlibrisgroup.com/KTH:KTH_LMSMILL12447056