graphs - users.encs.concordia.casthiel/comp352/2017_summer1/10… · graphs references 11/28...
TRANSCRIPT
Graphs
S. Thiel
Graphs
References
1/28
Graphs
S. Thiel1
1Department of Computer Science & Software EngineeringConcordia University
June 11, 2017
Graphs
S. Thiel
Graphs
References
2/28
Outline
Graphs
References
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
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 |
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
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)
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
Graphs
S. Thiel
Graphs
References
8/28
Undirected Graph Example
Figure: [1, p.372]
Graphs
S. Thiel
Graphs
References
9/28
Directed Graph Example
Figure: [1, p.372]
Graphs
S. Thiel
Graphs
References
10/28
Labeled Graph Example
Figure: [1, p.372]
I Also directed. Vertices have labels, edges have weights.
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
Graphs
S. Thiel
Graphs
References
12/28
Connected Component Example
Figure: [1, p.373]
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
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
Graphs
S. Thiel
Graphs
References
15/28
Adjacency Matrix Example
Figure: [1, p.374]
Graphs
S. Thiel
Graphs
References
16/28
Adjacency List Example
Figure: [1, p.374]
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
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)
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
Graphs
S. Thiel
Graphs
References
20/28
Graph Example
Figure: [1, p.384]
Graphs
S. Thiel
Graphs
References
21/28
Depth First Graph Example
Figure: [1, p.384]
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
Graphs
S. Thiel
Graphs
References
23/28
Graph Example Once More
Figure: [1, p.386]
Graphs
S. Thiel
Graphs
References
24/28
Breadth First Graph Example
Figure: [1, p.386]
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
Graphs
S. Thiel
Graphs
References
26/28
Topological Sort Example
Figure: [1, p.388]
I J1,J3,J2,J6,J4,J5,J7
Graphs
S. Thiel
Graphs
References
27/28
Topological Sort With Queue[1, p.388]
I Counting Edges.
Graphs
S. Thiel
Graphs
References
28/28
References I
[1] Clifford A. Shaffer.Data Structures and Algorithm Analysis in Java.2013.