Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S....

46
Graphs S. Thiel Graphs References 1/46 Graphs S. Thiel 1 1 Department of Computer Science & Software Engineering Concordia University July 31, 2019

Transcript of Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S....

Page 1: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

1/46

Graphs

S. Thiel1

1Department of Computer Science & Software EngineeringConcordia University

July 31, 2019

Page 2: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

2/46

Outline

Graphs

References

Page 3: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

3/46

Graphs [1, p.371]

I A very flexible data structure

I Commonly used to model both real world systems andabstract problems

Page 4: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

4/46

Terminology 1/5 [1, p372]

I Given a graph G = (V ,E ) consists of a set of verticesV and edges E

I Each edge in E is a connection between a pair ofvertices in V

I |V | is the number of vertices in G

I |E | is the number of edges in G

I |E | can range from zero to a maximum of |V |2 − |V |

Page 5: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

5/46

Multi-Edge and Edge-to-Self[1, p.372]

I We won’t consider these here

I They’re not prohibited by the general idea of graphs

Page 6: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

6/46

Terminology 2/5 [1, p.372]

I A graph that has relatively few edges is called sparse

I A graph that has many edges is called dense

I A graph with all possible edges is called complete(|V |2 − |V | edges)

Page 7: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

7/46

Terminology 3/5 [1, p.]

I Graphs can be either directed or undirected

I A directed graph is often call a digraph

I Graphs can be labeled and their edges can haveweights

Page 8: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

8/46

Undirected Graph Example

Figure: [1, p.372]

Page 9: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

9/46

Directed Graph Example

Figure: [1, p.372]

Page 10: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

10/46

Labeled Graph Example

Figure: [1, p.372]

I Also directed. Vertices have labels, edges have weights.

Page 11: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

11/46

Terminology 4/5 [1, p.372-373]

I Two vertices are adjacent if they share an edge

I adjacent vertices are neighbours

I An edge is said to be incident to its two connectedvertices

I A graph is connected if there is a path from any vertexto any other vertex

I The maximally connected subgraphs of an undirectedgraph are called connected components

I Not all vertices in a graph need be connected. Asubgraph G ′ of graph G is said to be maximallyconnected if there are no other vertices from G thatconnect to any vertices in G ′.

Page 12: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

12/46

Connected Component Example

Figure: [1, p.373]

Page 13: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

13/46

Terminology 5/5 [1, p.372]

I A sequence of vertices, each connected to the next byan edge, is called a path

I A path is simple if there are no duplicate vertices onthe path

I The length of a path is the number of vertices itcontains.

I A cycle is a path of length three or more that startsand ends at the same vertex

I A cycle is simple if there are no duplicate vertices inthe path save the first and the last

I A graph is Acyclic if it contains no cycles

I We call a Directed Acyclic Graph a (DAG)... how isthat different from a tree?

I A free tree is a connected, undirected graph with nosimple cycles, has |V | − 1 edges

Page 14: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

14/46

How do we Store a Graph[1, p.373–374]

I Adjacency Matrix: Great when lots of edgesI if undirected, only store half the matrixI still has Θ

(|V |2

)space complexity

I Adjacency List: Great when few edges, supports abunch of graph operations better

I directed, space complexity is Θ (|E |)I That means sparse graph space complexity is Θ (|V |)I That means dense graph space complexity is Θ

(|V |2

)

Page 15: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

15/46

Adjacency Matrix Example

Figure: [1, p.374]

Page 16: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

16/46

Adjacency List Example

Figure: [1, p.374]

Page 17: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

17/46

Operations[1, p.376-380]

I Setting/Getting Weights

I Marking vertices

I Counting edges/vertices

I On a given vertex we will want to know adjacentedges/vertices

Page 18: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

18/46

Traversing Graphs[1, p.380]

I graph traversal is similar to tree traversal

I We have PreVisit and PostVisit operations, likepre/post-order

I Three basic ways to traverseI depth-firstI breadth-firstI topological

I Sometimes we want to traverse all edges, mark-bit(painting)

Page 19: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

19/46

Depth-First [1, p.383-384]

I Using a Stack to go through a tree

I depth-first search tree

Page 20: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

20/46

Graph Example

Figure: [1, p.384]

Page 21: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

21/46

Depth First Graph Example

Figure: [1, p.384]

Page 22: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

22/46

Breadth-First [1, p.384]

I Using a Queue instead of a stack

I breadth-first search tree

Page 23: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

23/46

Graph Example Once More

Figure: [1, p.386]

Page 24: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

24/46

Breadth First Graph Example

Figure: [1, p.386]

Page 25: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

25/46

Topological Sort [1, p.387]

I Walk through all vertices in an order

I Scheduling tasks, do them in a linear order

I Necessarily a DAG

Page 26: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

26/46

Topological Sort Stack Example

Printing out vertices on post-visit, using a stack,recursion

Figure: [1, p.388]

I J1,J3,J2,J6,J4,J5,J7

Page 27: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

27/46

Topological Sort With Stack[1, p.388]

I Post-order traversal

I display items when popping off stack

Page 28: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

28/46

Topological Sort Example

Stack: J1,J2,J4,J5,J7

J1 J2

J3 J4

J5

J6

J7

Display:

Page 29: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

29/46

Topological Sort Example

Stack: J1,J2,J4,J5

J1 J2

J3 J4

J5

J6

J7

Display:J7

Page 30: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

30/46

Topological Sort Example

Stack: J1,J2,J4

J1 J2

J3 J4

J5

J6

J7

Display:J5,J7

Page 31: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

31/46

Topological Sort Example

Stack: J1,J2,J6

J1 J2

J3 J4

J5

J6

J7

Display:J4,J5,J7

Page 32: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

32/46

Topological Sort Example

Stack: J1,J2

J1 J2

J3 J4

J5

J6

J7

Display:J6,J4,J5,J7

Page 33: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

33/46

Topological Sort Example

Stack: J1,J3

J1 J2

J3 J4

J5

J6

J7

Display:J2,J6,J4,J5,J7

Page 34: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

34/46

Topological Sort Example

Stack: J1

J1 J2

J3 J4

J5

J6

J7

Display:J3,J2,J6,J4,J5,J7

Page 35: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

35/46

Topological Sort Example

Stack:

J1 J2

J3 J4

J5

J6

J7

Display:J1,J3,J2,J6,J4,J5,J7

Page 36: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

36/46

Topological Sort With Queue[1, p.388]

I Traverse Graph, assigning inbound edge counts tovertices

I Place vertices with inbound-edge count of 0 on thequeue

I Process the queue, each vertex is processed, then itsneighbours have their inbound-edge count reduced. Ifthat reduces the inbound edge-count on a vertex tozero, that vertex is added to the queue

I Continue processing queue till empty

I If queue empties before all vertices were printed, graphhad a cycle.

Page 37: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

37/46

Topological Sort (Queue) Example

Figure: [1, p.388]

I J1,J2,J3,J6,J4,J5,J7

Page 38: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

38/46

Topological Sort Example

Queue: J1

J1 J2

J3 J4

J5

J6

J70 1

1 2

2

1

1

Display:

Page 39: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

39/46

Topological Sort Example

Queue: J2,J3

J1 J2

J3 J4

J5

J6

J70 0

0 2

2

1

1

Display: J1

Page 40: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

40/46

Topological Sort Example

Queue: J3,J6

J1 J2

J3 J4

J5

J6

J70 0

0 1

1

0

1

Display: J1,J2

Page 41: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

41/46

Topological Sort Example

Queue: J6,J4

J1 J2

J3 J4

J5

J6

J70 0

0 0

1

0

1

Display: J1,J2,J3

Page 42: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

42/46

Topological Sort Example

Queue: J4

J1 J2

J3 J4

J5

J6

J70 0

0 0

1

0

1

Display: J1,J2,J3,J6

Page 43: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

43/46

Topological Sort Example

Queue: J5

J1 J2

J3 J4

J5

J6

J70 0

0 0

0

0

1

Display: J1,J2,J3,J6,J4

Page 44: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

44/46

Topological Sort Example

Queue: J7

J1 J2

J3 J4

J5

J6

J70 0

0 0

0

0

0

Display: J1,J2,J3,J6,J4,J5

Page 45: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

45/46

Topological Sort Example

Queue:

J1 J2

J3 J4

J5

J6

J70 0

0 0

0

0

0

Display: J1,J2,J3,J6,J4,J5,J7

Page 46: Graphs - users.encs.concordia.causers.encs.concordia.ca/~sthiel/coen352/09_Graphs.pdf · Graphs S. Thiel Graphs References 11/46 Terminology 4/5 [1, p.372-373] I Two vertices are

Graphs

S. Thiel

Graphs

References

46/46

References I

[1] Clifford A. Shaffer.Data Structures and Algorithm Analysis in Java.2013.