Data Structures in Java Lecture 16: Introduction to Graphs. - - PowerPoint PPT Presentation

data structures in java
SMART_READER_LITE
LIVE PREVIEW

Data Structures in Java Lecture 16: Introduction to Graphs. - - PowerPoint PPT Presentation

Data Structures in Java Lecture 16: Introduction to Graphs. 11/16/2015 Daniel Bauer 1 Graphs A Graph is a pair of two sets G=(V,E): V: the set of vertices (or nodes ) E: the set of edges . each edge is a pair (v,w) where v,w


slide-1
SLIDE 1

Data Structures in Java

Lecture 16: Introduction to Graphs.

11/16/2015 Daniel Bauer

1

slide-2
SLIDE 2
  • A Graph is a pair of two sets G=(V,E):
  • V: the set of vertices (or nodes)
  • E: the set of edges.
  • each edge is a pair (v,w) where

v,w ∈ V


Graphs

2

slide-3
SLIDE 3
  • A Graph is a pair of two sets G=(V,E):
  • V: the set of vertices (or nodes)
  • E: the set of edges.
  • each edge is a pair (v,w) where

v,w ∈ V


Graphs

v1 v3 v4 v5 v2 v6

3

slide-4
SLIDE 4
  • A Graph is a pair of two sets G=(V,E):
  • V: the set of vertices (or nodes)
  • E: the set of edges.
  • each edge is a pair (v,w) where

v,w ∈ V


Graphs

v1 v3 v4 v5 v2 v6

4

slide-5
SLIDE 5
  • A Graph is a pair of two sets G=(V,E):
  • V: the set of vertices (or nodes)
  • E: the set of edges.
  • each edge is a pair (v,w) where

v,w ∈ V


Graphs

v1 v3 v4 v5 v2 v6

V = {v1, v2, v3, v4, v5, v6 } E = {(v1, v2), (v1, v3), (v2, v3),(v2, v5),(v3, v4),
 (v3, v6),(v4, v5), (v4, v6), (v5, v6)}

5

slide-6
SLIDE 6
  • Graphs are used to model all kinds of relational data.
  • General purpose algorithms make it possible to solve

problems on these models.

  • Shortest Paths, Spanning Tree, Finding Cliques,

Strongly Connected Components, Network Flow, Graph Coloring, Minimum Edge/Vertex Cover, Graph Partitioning, …

Graphs in Computer Science

6

slide-7
SLIDE 7

Social Networks

7

slide-8
SLIDE 8

Interaction Networks Extracted from Text

http://www.cs.columbia.edu/~apoorv/SINNET/

8

slide-9
SLIDE 9

Rail Network

Source: Days of WonderVideo Games

9

slide-10
SLIDE 10

US Power Grid

10

slide-11
SLIDE 11

Human Disease Network

Source: Goh et al, PNAS 2007

11

slide-12
SLIDE 12

Graph-Based Representation

  • f Sentence Meaning

Source: Kevin Knight

“Pascale was charged with public intoxication and resisting arrest.”

12

slide-13
SLIDE 13

Graphical Models

rush hour bad weather accident sirens traffic jam

13

slide-14
SLIDE 14

Edges

  • Graphs may be directed or undirected.
  • In directed graphs, the edge pairs are ordered.
  • Edges often have some weight or cost associated

with them (weighted graphs).

v1 v3 v4 v5 v2 v6

E = {(v1, v3), (v2, v1),(v2, v3), (v3, v4), 
 (v3, v5), (v4, v6), (v5, v6)} V = {v1, v2, v3, v4, v5, v6 } directed graph

14

slide-15
SLIDE 15

Edges

  • Graphs may be directed or undirected.
  • In directed graphs, the edge pairs are ordered.
  • Edges often have some weight or cost associated

with them (weighted graphs).

v1 v3 v4 v5 v2 v6

E = {(v1, v3), (v2, v1),(v2, v3), (v3, v4), 
 (v3, v5), (v4, v6), (v5, v6)} V = {v1, v2, v3, v4, v5, v6 } directed and weighted graph

1 3 5 6 2 3 1

15

slide-16
SLIDE 16

Paths

  • Vertex w is adjacent to vertex v iff (w,v) ∈ E.
  • A path is a sequence of vertices w1, w2, …, wk


such that (wi, wi+1) ∈ E.

v1 v3 v4 v5 v2 v6

1 3 5 6 2 3 1

16

slide-17
SLIDE 17

Paths

v1 v3

v4

v5

v2

v6

1 3 5 6 2 3 1

Path from v1 to v6, length 3, cost 8 (v1, v3), (v3, v5), (v5, v6)

  • Vertex w is adjacent to vertex v iff (w,v) ∈ E.
  • A path is a sequence of vertices w1, w2, …, wk


such that (wi, wi+1) ∈ E.

  • length of a path: 


k-1 = number of edges on path

  • cost of a path:


Sum of all edge costs. 


17

slide-18
SLIDE 18

Simple Paths

v1 v3 v4 v5 v2 v6

18

slide-19
SLIDE 19

Simple Paths

v1 v3 v4 v5 v2 v6

  • A simple path is a path that contains every node only
  • nce (except possibly the first and last node).

18

slide-20
SLIDE 20

Simple Paths

v1 v3 v4 v5 v2 v6

  • A simple path is a path that contains every node only
  • nce (except possibly the first and last node).
  • (v2, v3, v4, v6, v5,v3, v1) is a path


but not a simple path.

18

slide-21
SLIDE 21

Simple Paths

v1 v3 v4 v5 v2 v6

  • A simple path is a path that contains every node only
  • nce (except possibly the first and last node).
  • (v2, v3, v4, v6, v5,v3, v1) is a path


but not a simple path.

  • There are only two simple paths between v2 and v1:

(v2, v1) and (v2, v3, v1)

18

slide-22
SLIDE 22

Simple Paths

v1 v3 v4 v5 v2 v6

  • A simple path is a path that contains every node only
  • nce (except possibly the first and last node).
  • (v2, v3, v4, v6, v5,v3, v1) is a path


but not a simple path.

  • There are only two simple paths between v2 and v1:

(v2, v1) and (v2, v3, v1)

  • (v1, v3, v2, v1) is a simple path.

18

slide-23
SLIDE 23
  • A cycle is a path (of length > 1) such that 


w1 = wk

  • (v3, v4, v6, v3) is a cycle.

Cycles in Directed Graphs

v1 v3 v4 v5 v2 v6

19

slide-24
SLIDE 24
  • A cycle is a path (of length > 1) such that 


w1 = wk

  • (v3, v4, v6, v3) is a cycle.

Cycles in Directed Graphs

v1 v3 v4 v5 v2 v6

  • A Directed Acyclic Graph (DAG) is a directed graph that

contains no cycles.

20

slide-25
SLIDE 25
  • A cycle is a path (of length > 1) such that 


w1 = wk

  • (v3, v4, v6, v3) is a cycle.

Cycles in Directed Graphs

v1 v3 v4 v5 v2 v6

  • A Directed Acyclic Graph (DAG) is a directed graph that

contains no cycles.

20

slide-26
SLIDE 26

Columbia CS Course Prerequisites as a DAG

Please do not use this figure for program planning! No guarantee for accuracy.

W1004 W3134 W1007 W3137 W3157 W3203 W3261 W4111 W4115 W4156 W4701

21

slide-27
SLIDE 27
  • An undirected graph is connected if there is a


path from every vertex to every other vertex.

Connectivity

connected graph

22

slide-28
SLIDE 28
  • An undirected graph is connected if there is a


path from every vertex to every other vertex.

Connectivity

unconnected graph

23

slide-29
SLIDE 29
  • A directed graph is weakly connected if there is

an undirected path from every vertex to every other vertex.

Connectivity in Directed Graphs

weakly connected graph

24

slide-30
SLIDE 30
  • A directed graph is strongly connected if there is

a path from every vertex to every other vertex.

Strongly Connected Graphs

v

Weakly connected, but not strongly connected (no other vertex can be reached from v).

25

slide-31
SLIDE 31
  • A directed graph is strongly connected if there is

a path from every vertex to every other vertex.

Strongly Connected Graphs

strongly connected

26

slide-32
SLIDE 32
  • A complete graph has edges between every pair
  • f vertices.

Complete Graphs

N=2

27

slide-33
SLIDE 33
  • A complete graph has edges between every pair
  • f vertices.

Complete Graphs

N=3

28

slide-34
SLIDE 34
  • A complete graph has edges between every pair
  • f vertices.

Complete Graphs

N=4

29

slide-35
SLIDE 35

Complete Graphs

N=5

  • A complete graph has edges between every pair
  • f vertices.

How many edges are there in a complete graph of size N?

30

slide-36
SLIDE 36

Complete Graphs

N=5

  • A complete graph has edges between every pair
  • f vertices.

How many edges are there in a complete graph of size N?

30

slide-37
SLIDE 37

Representing Graphs

  • Represent graph G = (E,V), option 1:
  • N x N Adjacency Matrix represented as 2-

dimensional Boolean[][].

  • A[u][v] = true if (u,v) ∈ E, else false

v0 v2 v3 v4 v1 v5

f f t f f f t f t f f f f f f t t f f f f f f t f f f f f t f f f f f f

0 1 2 3 4 5 1 2 3 4 5

31

slide-38
SLIDE 38

Representing Graphs

  • Represent graph G = (E,V), option 1:
  • N x N Adjacency Matrix represented as 2-

dimensional Integer[][].

  • A[u][v] = cost(u,v) if (u,v) ∈ E, else ∞

v0 v2 v3 v4 v1 v5

∞ ∞ 2 ∞ ∞ ∞ 1 ∞ 3 ∞ ∞ ∞ ∞ ∞ ∞ 5 4 ∞ ∞ ∞ ∞ ∞ ∞ 3 ∞ ∞ ∞ ∞ ∞ 4 ∞ ∞ ∞ ∞ ∞ ∞

0 1 2 3 4 5 1 2 3 4 5

1 2 3 5 4 4 3

32

slide-39
SLIDE 39

Representing Graphs

  • Problem of Adjacency Matrix representation:
  • For sparse graphs (that contain much less than 


|V|2 edges), a lot of array space is wasted.

v0 v2 v3 v4 v1 v5

0 1 2 3 4 5 1 2 3 4 5

1 2 3 5 4 4 3

∞ ∞ 2 ∞ ∞ ∞ 1 ∞ 3 ∞ ∞ ∞ ∞ ∞ ∞ 5 4 ∞ ∞ ∞ ∞ ∞ ∞ 3 ∞ ∞ ∞ ∞ ∞ 4 ∞ ∞ ∞ ∞ ∞ ∞

33

slide-40
SLIDE 40

Representing Graphs

  • Problem of Adjacency Matrix representation:
  • For sparse graphs (that contain much less than 


|V|2 edges), a lot of array space is wasted.

v0 v2 v3 v4 v1 v5

0 1 2 3 4 5 1 2 3 4 5

1 2 3 5 4 4 3

∞ ∞ 2 ∞ ∞ ∞ 1 ∞ 3 ∞ ∞ ∞ ∞ ∞ ∞ 5 4 ∞ ∞ ∞ ∞ ∞ ∞ 3 ∞ ∞ ∞ ∞ ∞ 4 ∞ ∞ ∞ ∞ ∞ ∞

Space requirement:

33

slide-41
SLIDE 41
  • Represent graph G = (E,V), option 2: Adjacency Lists
  • For each vertex, keep a list of all adjacent vertices.

Representing Graphs

v0 v2 v3 v4 v1 v5

1 2 3 5 4 4 3

v0 v1 v2 v3 v4 v5 v0:1 v2:3 v2:2 v3:3 v4:4 v5:3 v5:4

34

slide-42
SLIDE 42
  • Represent graph G = (E,V), option 2: Adjacency Lists
  • For each vertex, keep a list of all adjacent vertices.

Representing Graphs

v0 v2 v3 v4 v1 v5

1 2 3 5 4 4 3

v0 v1 v2 v3 v4 v5 v0:1 v2:3 v2:2 v3:3 v4:4 v5:3 v5:4 Space requirement:

34

slide-43
SLIDE 43

Storing Adjacency Lists

  • If we construct a graph (or read it in from some

specification), a LinkedList is better than an ArrayList because we don’t know how many adjacent vertices there are for each vertex.

  • Create an instance of a Vertex class for each vertex

and keep adjacency list in this object.

  • Can also keep an index to quickly access vertices

by name.

http://www.cs.columbia.edu/~bauer/cs3134/code/week11/BasicGraph.java

35