graphs Nov. 13, 2017 1 Example e g a c f d h b 2 Same - - PowerPoint PPT Presentation

graphs
SMART_READER_LITE
LIVE PREVIEW

graphs Nov. 13, 2017 1 Example e g a c f d h b 2 Same - - PowerPoint PPT Presentation

COMP 250 Lecture 28 graphs Nov. 13, 2017 1 Example e g a c f d h b 2 Same Example different notation e g a c f d h b 3 Weighted Graph e g 9 a 12 11 4 c f 5 7 7 d 2 4 h b 4 Definition A directed graph is


slide-1
SLIDE 1

1

COMP 250

Lecture 28

graphs

  • Nov. 13, 2017
slide-2
SLIDE 2

Example

2

f g c d a e b h

slide-3
SLIDE 3

Same Example – different notation

3

f g c d a e b h

slide-4
SLIDE 4

Weighted Graph

4

f g c d a e b h

12 2 7 9 11 4 7 4 5

slide-5
SLIDE 5

Definition

A directed graph is a set of vertices and set of ordered pairs of these vertices called edges. In an undirected graph, the edges are unordered pairs. 𝑊 = {𝑤𝑗 ∶ 𝑗 ∈ 1, … , 𝑜 } 𝐹 = { 𝑤𝑗 , 𝑤𝑘 ∶ 𝑗, 𝑘 ∈ 1, … , 𝑜 } 𝐹 = 𝑤𝑗, 𝑤𝑘 ∶ 𝑗, 𝑘 ∈ 1, … , 𝑜

slide-6
SLIDE 6

Examples

6

Vertices airports web pages Java objects Edges

slide-7
SLIDE 7

Examples

7

Vertices airports web pages Java objects Edges flights

slide-8
SLIDE 8

Examples

8

Vertices airports web pages Java objects Edges flights links (URLs)

slide-9
SLIDE 9

Examples

9

Vertices airports web pages Java objects Edges flights links (URLs) references

slide-10
SLIDE 10

linked lists graphs trees

slide-11
SLIDE 11

Terminology: “in degree”

11

f g c d a e b h v

a b c d e f g h

in degree

1 2 2 1 3 1

slide-12
SLIDE 12

Terminology: “out degree”

12

f g c d a e b h v

a b c d e f g h

  • ut degree

1 1 1 2 2 2 1

slide-13
SLIDE 13

Example: web pages

13

f g c d a e b h In degree: How many web pages link to some web page (e.g. f ) ? Out degree: How many web pages does some web page (e.g. f ) link to ?

slide-14
SLIDE 14

Terminology: path

14

f g c d a e b h

Examples

  • acfeb
  • dac
  • febf
  • …..

A path is a sequence of edges such that end vertex of one edge is the start vertex of the next edge and no vertex repeated except maybe first and last.

slide-15
SLIDE 15

Graph algorithms in COMP 251

Given a graph, what is the shortest (weighted) path between two vertices?

slide-16
SLIDE 16

Terminology: cycle

16

f g c d a e b h

Examples

  • febf
  • efe
  • fbf

A cycle is a path such that the last vertex is the same as the first vertex.

slide-17
SLIDE 17

g c d a

12 2 7 11 4 7

“Travelling Salesman” COMP 360

(Hamiltonian circuit)

Find the shortest cycle that visits all vertices

  • nce.

How many potential cycles are there in a graph of n vertices ?

slide-18
SLIDE 18

Directed Acyclic Graph

no cycles

a d c b

There are three paths from a to d. Used to capture dependencies.

slide-19
SLIDE 19

303 Software Design 302 Program Lang 273 Comp. Sys. 251 Data Str & Alg 350 Num. Meth 421 Data- bases 424 Artif. Intel. 310 Oper. Sys. 360 Alg. Design 330 Theory Comp. 206 Software Sys 250 Intro CompSci 202 Intro Program 240 Disc.

  • Str. 1

223 Linear Alg. 222 Cal III 323 Prob. SYSTEMS (compilers, networks, distributed sys, concurrency, web,..) APPLICATIONS (graphics, vision, bioinf, games, machine learning..) THEORY (crypto, optimization, game theory, logic, correctness, computability..) MATH (prereqs for many upper level COMP courses)

19

slide-20
SLIDE 20

Graph ADT

  • addVertex( … ), addEdge(…)
  • containsVertex( …), containsEdge(… )
  • getVertex( … ), getEdge( … )
  • removeVertex( …), removeEdge( …)
  • numVertices( ), numEdges( )

How to implement a Graph class? A graph is a generalization of a tree, so …

slide-21
SLIDE 21

Recall: How to implement a rooted tree in Java ?

// alternatively…. class TreeNode<T>{ T element; TreeNode<T> firstChild; TreeNode<T> nextSibling; : }

21

class Tree<T>{ TreeNode<T> root; : // inner class class TreeNode<T>{ T element; ArrayList< TreeNode<T> > children; TreeNode<T> parent; } }

slide-22
SLIDE 22

Adjacency List

(generalization of children for graphs)

22

f g c d a e b h v

a b c d e f g h

v.adjList

c f f a, c b, f b, e h

Here each adjacency list is sorted, but that is not always possible (or necessary).

slide-23
SLIDE 23

class Graph<T> { class Vertex<T> // Could have called it GNode { ArrayList<Vertex> adjList; T element; } } This is a very basic Graph class.

How to implement a Graph class in Java?

slide-24
SLIDE 24

class Graph<T> { class Vertex<T> { ArrayList<Edge> adjList; T element; boolean visited; } class Edge { Vertex endVertex; double weight; : } } Unlike a rooted tree, there is no notion of a root vertex in a graph.

How to implement a Graph class in Java?

slide-25
SLIDE 25

How to reference vertices?

Suppose we have a string name (key) for each vertex.

e.g. YUL for Trudeau airport, LAX for Los Angeles, …

class Graph<T> { HashMap< String, Vertex<T> > vertexMap; : class Vertex<T> { …} class Edge<T> { …} } We could also just enumerate vertices.

25

slide-26
SLIDE 26

How many objects ?

26

f e b

slide-27
SLIDE 27

27

Graph HashMap Vertex Vertex Edge Edge Edge Edge Edge Vertex ArrayList ArrayList ArrayList

f e b

slide-28
SLIDE 28

Adjacency Matrix

28

f c d a e b

a b c d e f a 0 0 1 0 0 0 b 0 0 0 0 0 1 c 0 0 0 0 0 1 d 1 0 1 0 0 0 e 0 1 0 0 0 1 f 0 1 0 0 1 0 boolean adjMatrix[ 6 ][ 6 ]

Assume we have a mapping from vertex names to 0, 1, …. , n-1.

slide-29
SLIDE 29

Adjacency Matrix

29

f c d a e b

a b c d e f a 1 0 1 0 0 0 b 0 0 0 0 0 1 c 0 0 0 0 0 1 d 1 0 1 0 0 0 e 0 1 0 0 1 1 f 0 1 0 0 1 0 boolean adjMatrix[ 6 ][ 6 ]

loop

slide-30
SLIDE 30

Suppose a graph has 𝑜 vertices. The graph is dense if number of edges is close to 𝑜2. The graph is sparse if number of edges is close to 𝑜. (These are not formal definitions.)

slide-31
SLIDE 31

Exercise

Would you use an adjacency list or adjacency matrix for each of the following?

  • The graph is sparse e.g. 10,000 vertices and 20,000

edges and we want to use as little space as possible.

  • The graph has 10,000 vertices and 20,000,000 edges,

and it is important to use as little space as possible.

  • You need to answer the query areAdjacent() as quickly

as possible, no matter how much space you use.

  • You need to perform operation insertVertex.
  • You need to perform operation removeVertex.
slide-32
SLIDE 32

Exercise

Would you use an adjacency list or adjacency matrix for each of the following?

  • The graph is sparse e.g. 10,000 vertices and 20,000

edges and we want to use as little space as possible.

  • The graph is dense e.g. 10,000 vertices and 20,000,000

edges, and we want to use as little space as possible.

  • You need to answer the query areAdjacent() as quickly

as possible, no matter how much space you use.

  • You need to perform operation insertVertex.
  • You need to perform operation removeVertex.
slide-33
SLIDE 33

Exercise

Would you use an adjacency list or adjacency matrix for each of the following?

  • The graph is sparse e.g. 10,000 vertices and 20,000

edges and we want to use as little space as possible.

  • The graph is dense e.g. 10,000 vertices and 20,000,000

edges, and we want to use as little space as possible. .

  • Answer the query areAdjacent() as quickly as possible,

no matter how much space you use.

  • You need to perform operation insertVertex.
  • You need to perform operation removeVertex.
slide-34
SLIDE 34

Exercise

Would you use an adjacency list or adjacency matrix for each of the following?

  • The graph is sparse e.g. 10,000 vertices and 20,000

edges and we want to use as little space as possible.

  • The graph is dense e.g. 10,000 vertices and 20,000,000

edges, and we want to use as little space as possible. .

  • Answer the query areAdjacent() as quickly as possible,

no matter how much space you use.

  • Perform operation insertVertex( v ).
  • You need to perform operation removeVertex.
slide-35
SLIDE 35

Exercise

Would you use an adjacency list or adjacency matrix for each of the following?

  • The graph is sparse e.g. 10,000 vertices and 20,000

edges and we want to use as little space as possible.

  • The graph is dense e.g. 10,000 vertices and 20,000,000

edges, and we want to use as little space as possible. .

  • Answer the query areAdjacent() as quickly as possible,

no matter how much space you use.

  • Perform operation insertVertex( v ).
  • Perform operation removeVertex( v ).
slide-36
SLIDE 36

Next lecture

  • Recursive graph traversal
  • depth first
  • Non-recursive graph traversal
  • depth first
  • breadth first

36