graphs - users.encs.concordia.casthiel/comp352/2017_summer1/10… · graphs references 11/28...

28
Graphs S. Thiel Graphs References 1/28 Graphs S. Thiel 1 1 Department of Computer Science & Software Engineering Concordia University June 11, 2017

Upload: others

Post on 09-Oct-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

1/28

Graphs

S. Thiel1

1Department of Computer Science & Software EngineeringConcordia University

June 11, 2017

Page 2: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

2/28

Outline

Graphs

References

Page 3: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

3/28

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.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

4/28

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 grom zero to a maximum of |V |2 − |V |

Page 5: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

5/28

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

I We won’t consider these here

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

Page 6: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

6/28

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.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

7/28

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.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

8/28

Undirected Graph Example

Figure: [1, p.372]

Page 9: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

9/28

Directed Graph Example

Figure: [1, p.372]

Page 10: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

10/28

Labeled Graph Example

Figure: [1, p.372]

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

Page 11: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

11/28

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

Page 12: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

12/28

Connected Component Example

Figure: [1, p.373]

Page 13: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

13/28

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.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

14/28

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

I Adjacency Matrix: Great when lots of edges

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

Page 15: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

15/28

Adjacency Matrix Example

Figure: [1, p.374]

Page 16: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

16/28

Adjacency List Example

Figure: [1, p.374]

Page 17: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

17/28

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/nodes

Page 18: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

18/28

Traversing Graphs[1, p.380]

I graph traversal is similar to tree traversal

I We have PreVisit and PostVisit operations, likepre/port-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.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

19/28

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.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

20/28

Graph Example

Figure: [1, p.384]

Page 21: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

21/28

Depth First Graph Example

Figure: [1, p.384]

Page 22: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

22/28

Breadth-First [1, p.384]

I Using a Queue instead of a stack

I breadth-first search tree

Page 23: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

23/28

Graph Example Once More

Figure: [1, p.386]

Page 24: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

24/28

Breadth First Graph Example

Figure: [1, p.386]

Page 25: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

25/28

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

I Printing out nodes on post-visit, using a stack,recursion

Page 26: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

26/28

Topological Sort Example

Figure: [1, p.388]

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

Page 27: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

27/28

Topological Sort With Queue[1, p.388]

I Counting Edges.

Page 28: Graphs - users.encs.concordia.casthiel/comp352/2017_Summer1/10… · Graphs References 11/28 Terminology 4/5 [1, p.372-373] I Two vertices are adjacent if they share an edge I adjacent

Graphs

S. Thiel

Graphs

References

28/28

References I

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