CSY2087 Graphs
CSY2087 Graphs
CSY2087
Graphs
Objectives
• Learn about graphs
• Learn the basic terminology of graph theory
• Represent graphs in computer memory
• Explore graphs as ADTs
Objectives
• Examine and implement various graph
traversal algorithms
• Implement the shortest path algorithm
Introduction
• Königsberg bridge problem:
– The river Pregel flows around the island Kneiphof
and then divides into two branches
Introduction
• Starting at one land area,
can you cross all bridges
exactly once and return
to the start?
– In 1736, Euler represented
the problem as a graph
and answered the
question: No
Introduction
• Over the past 200 years, graph theory has
been applied to a variety of problems,
including:
• Model electrical circuits, chemical compounds,
highway maps, etc.
• Analysis of electrical circuits, finding the shortest
route, project planning, linguistics, genetics, social
science, etc.
Modeling Using Graphs
Seattle
2097 Boston
983
Chicago
1331 214
807
1003 New York
787
Denver
533
1267 1260
599
888
San Francisco 1015 Kansas City
381 1663
864
496
Los Angeles 1435 Atlanta
781
810
Dallas
661
239
Houston 1187
Miami
Graph Definitions and Notations
• a X: a is an element of the set X
• Subset (Y X): every element of Y is also an
element of X
• Intersection (A B): contains all the elements
in A and B
– A B = x | x A and x B
• Union (A B): set of all the elements that are
in A or in B
– A B = x | x A or x B
Graph Definitions and Notations
• A B: set of all the ordered pairs of elements
of A and B
– A B = (a, b) | a A, b B
• Graph G: G = (V, E)
– V is a finite nonempty set of vertices of G
– EVV
– Elements in E are the pairs of elements of V
– E is called set of edges
Graph Definitions and Notations
• Directed graph or digraph: elements of E(G)
are ordered pairs
• Undirected graph: elements not ordered pairs
• If (u, v) is an edge in a directed graph
– Origin: u
– Destination: v
• Subgraph H of G: if V(H) V(G) and E(H)
E(G)
• Every vertex and edge of H is in G
Graph Definitions and Notations
Graph Definitions and Notations
Graph Definitions and Notations
• Adjacent: there is an edge from one vertex to
the other; i.e., (u, v) E(G)
• Incident: if edge e = (u, v) then e is incident on
u and v
– Loop: edge incident on a single vertex
• Parallel edges: associated with the same pair
of vertices
• Simple graph: has no loops or parallel edges
Graph Definitions and Notations
• Path: sequence of vertices u1, u2, ..., un such
that u = u1, un = v, and (ui, ui + 1) is an edge for
all i = 1, 2, ..., n − 1
• Connected vertices: there is a path from u to v
• Simple path: path in which all vertices, except
possibly the first and last, are distinct
• Cycle: simple path in which the first and last
vertices are the same
Graph Definitions and Notations
• Connected: path exists from any vertex to any
other vertex
– Component: maximal subset of connected
vertices
• In a connected graph G, if there is an edge
from u to v, i.e., (u, v) E(G), then u is
adjacent to v and v is adjacent from u
• Strongly connected: any two vertices in G are
connected
Graph Representations
• To write programs that process and
manipulate graphs
– Must store graphs in computer memory
• A graph can be represented in several ways:
– Adjacency matrices
– Adjacency lists
Adjacency Matrix
• G: graph with n vertices (n 0)
– V(G) = v1, v2, ..., vn
• Adjacency matrix (AG of G): two-‐dimensional n
n matrix such that:
dfs(vertex v) {
visit v;
for each neighbor w of v
if (w has not been visited)
{
dfs(w);
}
}
Depth-First Search Example
0 1 0 1 0 1
2 2 2
3 4 3 4 3 4
0 1 0 1
2 2
3 4 3 4
Breadth First Traversal
• Breadth first traversal of a graph
– Similar to traversing a binary tree level by level
– Nodes at each level are visited from left to right
• Starting at the first vertex, the graph is
traversed as much as possible
– Then go to next vertex not yet visited
• Use a queue to implement the breadth first
search algorithm
Breadth-First Search General
Algorithm
a. for each vertex v in the graph
if v is not visited
add v to the queue
b. Mark v as visited
c. while the queue is not empty
1. Remove vertex u from the queue
if w is not visited
1. Add w to the queue
2. Mark w as visited
Breadth-First Search Algorithm
bfs(vertex v) {
create an empty queue for storing vertices to be visited;
add v into the queue;
mark v visited;
while the queue is not empty {
dequeue a vertex, say u, from the queue
visit u;
for each neighbor w of u
if w has not been visited {
add w into the queue;
mark w visited;
}
}
}
Breadth-First Search Example
0 1 0 1 0 1
2 2 2
3 4 3 4 3 4
Example
• https://www.cs.usfca.edu/~galles/visualization/DFS.html
• https://www.cs.usfca.edu/~galles/visualization/
Dijkstra.html
Miscellaneous-Extra Slides
• Alternative Graph representations/code
(From Book by Daniel Liang – chapter of
ebook available)
Representing Graphs
Representing Vertices
Representing Edges: Edge Array
Representing Edges: Edge Objects
Representing Edges: Adjacency Matrices
Representing Edges: Adjacency Lists
Modeling Graphs
UnweightedGraph
Graph AbstractGraph
WeightedGraph
Seattle
2097 Boston
983
Chicago
1331 214
807
1003 New York
787
Denver
533
1267 1260
599
888
San Francisco 1015 Kansas City
381 1663
864
496
Los Angeles 1435 Atlanta
781
810
Dallas
661
239
Houston 1187
Miami
Applications of the DFS
Detecting whether a graph is connected. Search the graph starting
from any vertex. If the number of vertices searched is the same as
the number of vertices in the graph, the graph is connected.
Otherwise, the graph is not connected
2097 Boston
983
Chicago
1331 214
807
1003 New York
787
Denver
533
1267 1260
599
888
San Francisco 1015 Kansas City
381 1663
864
496
Los Angeles 1435 Atlanta
781
810
Dallas
661
239
Houston 1187
Miami
Applications of the BFS
Detecting whether a graph is connected. A graph is connected if there is a
path between any two vertices in the graph.
Finding a shortest path between two vertices. You can prove that the path
between the root and any node in the BFS tree is the shortest path between
the root and the node