0% found this document useful (0 votes)
103 views102 pages

Graph: Dr. Krishan Kumar Assistant Professor & Head Computer Science & Engineering

The document discusses graphs and graph theory concepts. It introduces graphs, including directed and weighted graphs. It defines graph representations like adjacency lists and matrices. It also defines common graph terminology such as paths, connected graphs, subgraphs, and graph types including complete graphs, bipartite graphs, and Hamiltonian and Euler cycles.

Uploaded by

Harkirat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views102 pages

Graph: Dr. Krishan Kumar Assistant Professor & Head Computer Science & Engineering

The document discusses graphs and graph theory concepts. It introduces graphs, including directed and weighted graphs. It defines graph representations like adjacency lists and matrices. It also defines common graph terminology such as paths, connected graphs, subgraphs, and graph types including complete graphs, bipartite graphs, and Hamiltonian and Euler cycles.

Uploaded by

Harkirat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 102

Graph

Dr. Krishan Kumar


Assistant Professor & Head
Computer Science & Engineering

1 CSL 201 Data Structures (ODD Semester) 12/29/20


Content
 Graph
 Introduction
 DFS
 BFS
 Topological sort

2 CSL 201 Data Structures (ODD Semester) 12/29/20


Graphs
Definition
(undiredted,
unweighted):
◦ A Graph G, consists
of
◦ a set of vertices, V A labeled simple graph:
◦ a set of edges, E Vertex set V = {1, 2, 3, 4, 5, 6}
◦ where each edge is Edge set E = {{1,2}, {1,5}, {2,3}, {2,5
associated with a {3,4}, {4,5}, {4,6}}.
pair of vertices.
We write: G = (V, E)
Graphs
Directed Graph:
◦ Same as above, but
where each edge is
associated with an
ordered pair of vertices.

A labeled simple graph:


Vertex set V = {2,3,5,7,8,9,10,11}
Edge set E = {{3,8}, {3,10}, {5,11},
{7,8}, {7,11}, {8,9}, {11,2},{11,9},
{11,10}}.
Graphs
Weighted Graph:
◦ Same as above, but
where each edge also
has an associated real
number with it, known as
the edge weight.
A labeled weighted graph
Vertex set V = {1,2,3,4,5}
Data Structures to Store Graphs
Edge List Structure Surprisingly, if you implement these
Each vertex object contains a label two lists with doubly linked lists, our
for that vertex. performance for many operations is
Each edge object contains a label quite reasonable:
for that edge, as well as two 1) Accessing information about the
references: graph such as its size, number of
 one to the starting vertex and one to vertices etc. can be done in
the ending vertex of that edge. constant time by keeping counters
 (In an undirected graph the designation in the data structure.
of starting and ending is unnecessary.) 2) One can loop through all vertices
and edges in O(V) or O(E) time,
respectively.
3) Insertion of edges and vertices can
be done in O(1) time, assuming you
already have access to the two
vertices you are connecting with an
edge.
4) Access to edges incident to a vertex
takes O(E) time, since all edges
have to be inspected.
Data Structures to Store Graphs
Adjacency Matrix Structure
Certain operations are slow using just an
adjacency list because one does not have quick
access to incident edges of a vertex.
We can add to the Adjacency List structure:
 a list of each edge that is incident to a vertex stored at
that vertex.
 This gives the direct access to incident edges that speeds
up many algorithms.
 eg. In Dijkstra's we always update estimates by looking
at each edge incident to a particular vertex.)
Data Structures to Store Graphs
Adjacency Matrix
The standard adjacency matrix
stores a matrix as a 2-D array with
each slot in A[i][j] being a 1 if there is
an edge from vertex i to vertex j, or
storing a 0 otherwise.
 Can have a more O-O design:
 Each entry in the array is null if no edge
is connecting those vertices, or an
Edge object that stores all necessary
information about the edge. 1 2 3 4 5 6
1 0 1 0 0 1 0
 Although these are very easy to work with
mathematically, they are more inefficient 2 1 0 1 0 1 0
than Adjacency lists for several tasks. For 3 0 1 0 1 0 0
example, you must scan all vertices to
4 0 0 1 0 1 1
find all the edges incident to a vertex.
 In a relatively sparse graph, using an 5 1 1 0 1 0 0
adjacency matrix would be very 6 0 0 0 1 0 0
inefficient running Dijkstra's algorithm,
for example.
Graph Definitions – types of Graphs
 A complete undirected  A graph is bipartite
unweighted graph  if there exists a way to
 is one where there is an partition the set of vertices V,
edge connecting all possible in the graph into two sets V1
pairs of vertices in a graph. and V2
The complete graph with n  where V1  V2 = V and V1 
vertices is denoted as Kn. V2 = , such that each edge
in E contains one vertex
from V1 and the other vertex
from V2.
Graph Definitions – types of graphs
 Complete bipartite graph
 A complete bipartite graph
on m and n vertices is
denoted by Km,n and consists
of m+n vertices, with each of
the first m vertices
connected to all of the other
n vertices, and no other
vertices.
Graph Definitions – graph terms
 A path
 A path of length n from vertex
v0 to vertex vn is an alternating
sequence of n+1 vertices and
n edges beginning with vertex
v0 and ending with vertex vn in
which edge ei is incident
upon vertices vi-1 and vi.
 (The order in which these are
connected matters for a path in a
directed graph in the natural way.)

 A connected graph
 A connected graph is one
where any pair of vertices in
the graph is connected by at
least one path.
Graph Definitions – types of Graphs
 A weighted graph  The function dist(v,w)
 A weighted graph associates  The function dist(v, w),
a label (weight) with every where v and w are two
edge in the graph. vertices in a graph, is
 The weight of a path or the
defined as the length of
weight of a tree in a weighted the shortest path from v
graph is the sum of the to w.
weights of the selected edges.  dist(b,e) = 8
Graph Definitions – types of Graphs
 A subgraph
 A graph G'= (V', E') is a
subgraph of G = (V, E) if
V'  V, E'  E, and for
every edge e'  E', if e'
is incident on v' and w',
then both of these
vertices is contained in
V'.
Graph Definitions – types of Graphs
 A simple path
6
 A simple path is one that contains
4 5
no repeated vertices.
1
3 2

 A cycle
 A path of non-zero length from 6
and to the same vertex with no 4 5
repeated edges.
1
 A simple cycle
3 2
 A cycle with no repeated vertices
except for the first and last one.
Graph Definitions – types of Graphs
 A Hamiltonian cycle
 A Hamiltonian cycle is a simple
4 5
cycle that contains all the
vertices in the graph.
6 1
3 2
 A Euler cycle
An Euler cycle is a
cycle that contains
every edge in the
graph exactly once.
 Note that a vertex may
be contained in an Euler
cycle more than once.
Typically, these are
known as Euler circuits,
because a circuit has no
repeated edges.
Euler Circuit vs Hamiltonian Cycle
Interestingly enough, there is a nice simple
method for determining if a graph has an Euler
circuit
but no such method exists to determine if a
graph has a Hamiltonian cycle.
The latter problem is an NP-Complete problem.
 In a nutshell, this means it is most-likely difficult to solve
perfectly in polynomial time. We will cover this topic at
the end of the course more thoroughly, hopefully.
Graph Definitions – types of Graphs
 The complement of a graph
1 3 5
The complement of a
graph G is a graph G' 6
which contains all the 4
vertices of G, but for each 2
edge that exists in G, it is
NOT in G', and for each 2 3 4 5 6 2 3 4 5 6
possible edge NOT in G, it
1 1 0 0 1 0 1 0 1 1 0 1
IS in G'.
2 1 0 1 0 2 0 1 0 1
3 1 0 0 3 0 1 1
 Two graphs G and G' are
4 1 1 4 0 0
isomorphic if there is a
5 0 5 1
one-to-one correspondence
between the vertices of the
two graphs such that the
resulting adjacency
matrices are identical.
Graph Coloring
For graph coloring, we will deal with unweighted
undirected graphs.
To color a graph, assign a color to each vertex such
that no two vertices connected by an edge are the
same color.
 Thus, a graph where all vertices are connected (a complete
graph) must have all of its vertices colored separate colors.
Graph Coloring
All bipartite graphs can be colored with only two colors, and
all graphs that can be colored with two colors are bipartite.
To see this, first simply note that we can two-color a bipartite
graph by simply coloring all the vertices in V1 one color and all
the vertices in V2 the other color.
 To see the latter result, given a two-coloring of a graph, simply separate
the vertices by color, putting all blue vertices on one side and all the red
ones on the other. These two groups specify the existence of sets V 1 and
V2, as designated by the definition of bipartite graphs.
Graph Coloring
Chromatic number
The minimum number of colors that is necessary to
color a graph

Interestingly enough…
there is an efficient solution to determine whether or
not a graph can be colored with two colors or not,
 but no efficient solution currently exists to determine whether
or not a graph can be colored using three colors.
Graph Traversals – Depth First Search
The general "rule" used in searching a graph using a depth
first search is to:
1) search down a path from a particular source vertex as far
as you can go.
2) When you can go to farther, "backtrack" to the last vertex
from which a different path could have been taken.
3) Continue in this fashion, attempting to go as deep as
possible down each path until each node has been visited.

 The most difficult part of this algorithm is keeping track of


what nodes have already been visited, so that the algorithm
does not run ad infinitum. We can do this by labeling each
visited node and labeling "discovery" and "back" edges.
Graph Traversals – Depth First Search
 The algorithm is as follows:
DFS(Graph G,vertex v):
For all edges e incident to
the start vertex v do:
1) If e is unexplored
a) Let e connect v to w.
b) If w is unexplored
i) Label e as a discovery edge
ii) Recursively call DFS(G,w)
else
iii) Label e as a back edge
Graph Traversals –
Depth First Search (DFS)
To prove that this algorithm visits all vertices in the
connected component of the graph in which it
starts, note the following:
Let the vertex u be the first vertex on any path from
the source vertex that is not visited.
 That means that w, which is connected to u was visited, but by
the algorithm given, it's clear that if this situation occurs, u
must be visited, contradicting the assumption that u was
unvisited.
Next, we must show that the algorithm terminates.
 If it does not, then there must exist a "search path" that never
ends.
 But this is impossible. A search path ends when an already
visited vertex is visited again. The longest path that exists
without revisiting a vertex is of length V, the number of
vertices in the graph.
Graph Traversals –
Depth First Search (DFS)

The running time of DFS is O(V+E).


To see this, note that each edge and vertex is visited
at most twice. In order to get this efficiency, an
adjacency list must be used. (An adjacency matrix
can not be used to complete this algorithm that
quickly.)
Undirected Graphs
D
In undirected graphs, edges have no specific
A
direction C
 Edges are always "two-way"
B

Thus, (u, v) ∊ E implies (v, u) ∊ E.


Only one of these edges needs to be in the set
The other is implicit, so normalize how you check
for it

Degree of a vertex: number of edges containing


that vertex
25 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
 Put another way: the number of adjacent vertices
Directed Graphs
In directed graphs (or digraphs), edges have direction
D
D
A A
C or C
B B

2 edges here

Thus, (u, v) ∊ E does not imply (v, u) ∊ E.

Let (u, v)  E mean u → v


 Call u the source and v the destination
 In-Degree of a vertex: number of in-bound edges (edges
where the vertex is the destination)
 Out-Degree of a vertex: number of out-bound edges (edges
where the vertex is the source)
26 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Self-Edges, Connectedness
A self-edge a.k.a. a loop edge is of the form (u, u)
 The use/algorithm usually dictates if a graph has:
No self edges
Some self edges
All self edges

A node can have a(n) degree / in-degree / out-


degree of zero

A graph does not have to be connected


Even if every node has non-zero degree
More discussion of this to come
27 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
D

More Notation A
C
For a graph G = (V, E):
|V| is the number of vertices B

|E| is the number of edges V = {A, B, C, D}


Minimum? E = {(C, B), (A, B),
Maximum for undirected? (B, A), (C, D)}
Maximum for directed?

If (u, v) ∊ E , then v is a neighbor of u (i.e., v is adjacent to


u)
Order matters for directed edges:
u is not adjacent to v unless (v, u)  E
28 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
D

More Notation A
C
For a graph G = (V, E):
|V| is the number of vertices B

|E| is the number of edges


Minimum? 0
Maximum for undirected? |V||V+1|/2  O(|V|2)
Maximum for directed? |V|2  O(|V|2)

If (u, v) ∊ E , then v is a neighbor of u (i.e., v is adjacent to


u)
Order matters for directed edges:
u is not adjacent to v unless (v, u)  E
29 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Examples Again
Which would use directed edges?
Which would have self-edges?
Which could have 0-degree nodes?
Web pages with links
Facebook friends
"Input data" for the Kevin Bacon game
Methods in a program that call each other
Road maps
Airline routes
Family trees
30 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Course pre-requisites
Weighted Graphs
In a weighted graph, each edge has a weight or cost
 Typically numeric (ints, decimals, doubles, etc.)
 Orthogonal to whether graph is directed
 Some graphs allow negative 20
Clinton
weights; many do not
Mukilteo

Kingston 30 Edmonds

Bainbridge 35
Seattle

60
Bremerton
31 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Examples Again
What, if anything, might weights represent for each
of these?
Do negative weights make sense?

Web pages with links


Facebook friends
"Input data" for the Kevin Bacon game
Methods in a program that call each other
Road maps
Airline routes
Family trees
32 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Course pre-requisites
Paths and Cycles
We say "a path exists from v0 to vn" if there is a list
of vertices [v0, v1, …, vn] such that (vi,vi+1) ∊ E for all 0
 i<n.
Chicago
A cycle is a path that begins and ends at the same
Seattle
node (v0==vn)
Salt Lake City

San Francisco
Dallas
Example path (that also happens to be a cycle):
[Seattle, Salt Lake City, Chicago, Dallas, San Francisco, Seattle]
33 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Path Length and Cost
Path length: Number of edges in a path
Path cost: Sum of the weights of each edge

Example where
P= [ Seattle, Salt3.5Lake City, Chicago
Chicago, Dallas,
Seattle San Francisco, Seattle]
2 2 length(P) = 5
cost(P) = 11.5
2 Salt Lake City
2.5
2.5 2.5 Length is sometimes
called "unweighted cost"
3
San Francisco Dallas
34 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Simple
A simple path Paths and
repeats no Cycles
vertices (except the first
might be the last):
[Seattle, Salt Lake City, San Francisco, Dallas]
[Seattle, Salt Lake City, San Francisco, Dallas, Seattle]

A cycle is a path that ends where it begins:


[Seattle, Salt Lake City, Seattle, Dallas, Seattle]

A simple cycle is a cycle and a simple path:


[Seattle, Salt Lake City, San Francisco, Dallas, Seattle]

35 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Paths
Example: and Cycles inDDirected Graphs
C
A

B
Is there a path from A to D? No

Does the graph contain any cycles? No

36 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Undirected Graph Connectivity
An undirected graph is connected if for all
pairs of vertices u≠v, there exists a path from u to v

Connected graph Disconnected graph

An undirected graph is complete,


or fully connected, if for all pairs
of vertices u≠v there exists an
edge from u to v

37 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Directed
A directed graph Graph
is strongly Connectivity
connected if
there is a path from every vertex to every
other vertex

A directed graph is weakly connected if


there is a path from every vertex to every
other vertex ignoring direction of edges

A direct graph is complete or fully


connected, if for all pairs of vertices u≠v ,
there exists an edge from u to v

38 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Examples Again
For undirected graphs: connected?
For directed graphs: strongly connected?
weakly connected?

Web pages with links


Facebook friends
"Input data" for the Kevin Bacon game
Methods in a program that call each other
Road maps
Airline routes
Family trees
39 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Course pre-requisites
WhenTrees
talkingas
about Graphs
graphs, we say a tree
is a graph that is: D E
undirected
B
acyclic
connected A

All trees are graphs, but NOT all graphs C


are trees
F

How does this relate to the trees we know G H


and "love"?

40 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Rooted Trees
We are more accustomed to rooted trees where:
We identify a unique root
We think of edges as directed:DparentE to children
B
Picking a root gives a unique A
A
rooted tree
B C
The tree is simply drawn C
differently and with F D E F
undirected edges
G H G H

41 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Rooted Trees
We are more accustomed to rooted trees where:
We identify a unique root
We think of edges as directed:DparentE to children
F
B
Picking a root gives a unique
A G H C
rooted tree
The tree is simply drawn C A

differently and with F B


undirected edges
G H D E

42 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Directed Acyclic Graphs (DAGs)
A DAG is a directed graph with no directed cycles
Every rooted directed tree is a DAG
But not every DAG is a rooted directed tree

Every DAG is a directed graph


But not every directed graph is a DAG

43 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Examples Again
Which of our directed-graph examples do you
expect to be a DAG?

Web pages with links


Facebook friends
"Input data" for the Kevin Bacon game
Methods in a program that call each other
Road maps
Airline routes
Family trees
Course pre-requisites
44 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Density / Sparsity
Recall:
In an undirected graph, 0≤|E|< |V|2
Recall:
In a directed graph, 0≤|E|≤|V|2

So for any graph, |E| is O(|V|2)

Another fact:
If an undirected graph is connected, then |E| ≥
|V|-1 (pigeonhole principle)
45 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Density / Sparsity
|E| is often much smaller than its maximum size

We do not always approximate as |E| as O(|V|2)


This is a correct bound, but often not tight

If |E| is (|V|2) (the bound is tight), we say the


graph is dense
More sloppily, dense means "lots of edges"

If |E| is O(|V|) we say the graph is sparse


More sloppily, sparse means "most possible
46 CSEedges missing"
332 Data Abstractions, Summer 2012 July 23, 2012
What’s the Data Structure?
Graphs are often useful for lots of data and questions
 Example: "What’s the lowest-cost path from x to y"

But we need a data structure that represents graphs

Which data structure is "best" can depend on:


 properties of the graph (e.g., dense versus sparse)
 the common queries about the graph ("is (u ,v) an
edge?" vs "what are the neighbors of node u?")

We will discuss two standard graph representations


 Adjacency Matrix and Adjacency List
47  Different
CSE trade-offs,
332 Data Abstractions, Summer particularly
2012 time versus space
July 23, 2012
Adjacency Matrix
Assign each node a number from 0 to |V|-1
A |V| x |V| matrix of Booleans (or 0 vs. 1)
 Then M[u][v] == true means there is an edge
from u to v
D A B C D
A A F T F F
C
B T F F F
B
C F T F T
D F F F F

48 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Adjacency Matrix Properties
Running time to:
 Get a vertex’s out-edges:
 Get a vertex’s in-edges:
 Decide if some edge exists: A B C D
 Insert an edge:
A F T F F
 Delete an edge:
B T F F F
C F T F T
Space requirements: D F F F F

49
Best for sparse or dense graphs?
CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Adjacency Matrix Properties
Running time to:
 Get a vertex’s out-edges: O(|V|)
 Get a vertex’s in-edges: O(|V|)
 Decide if some edge exists: O(1)
A B C D
 Insert an edge: O(1)
A F T F F
 Delete an edge: O(1)
B T F F F
C F T F T
Space requirements:
D F F F F
O(|V| )2

50 Best for sparse or dense graphs? dense


CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Adjacency Matrix Properties
How will the adjacency matrix vary for an
undirected graph?
Will be symmetric about diagonal axis
Matrix: Could we save space byA B C D
using only
about half the array? A F T F F
B T F F F
C F T F T
D F F T F

CSE 332 Data Abstractions, Summer 2012


51
But how would you "get all neighbors"? July 23, 2012
Adjacency Matrix Properties
How can we adapt the representation for weighted
graphs?
Instead of Boolean, store a number in each cell
Need some value to represent ‘not an edge’
0, -1, or some other value based on how you are
using the graph
Might need to be a separate field if no restrictions
on weights

52 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Adjacency List
Assign each node a number from 0 to |V|-1
An array of length |V| in which each entry stores a
list of all adjacent vertices (e.g., linked list)

D
A B /
A
C
B A /
B
C D B /

D /

53 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Adjacency List Properties
A B /
Running time to:
 Get a vertex’s out-edges: B A /

 Get a vertex’s in-edges: C D B /

 Decide if some edge exists: D /


 Insert an edge:

 Delete an edge:

Space requirements:

54 Best for sparse or dense graphs?


CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Adjacency List Properties
Running time to:
 Get a vertex’s out-edges:
O(d) where d is out-degree of vertex
 Get a vertex’s in-edges:
O(|E|) (could keep a second adjacency list for this!)
 Decide if some edge exists:
O(d) where d is out-degree of source
 Insert an edge:
O(1) (unless you need to check if it’s already there)
 Delete an edge:
O(d) where d is out-degree of source

Space requirements: O(|V|+|E|)

55 Best for sparse or dense graphs? sparse


CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Undirected Graphs
Adjacency lists also work well for undirected graphs
with one caveat
Put each edge in two lists to support efficient "get
all neighbors"

A B /
D
A B C / A /
C
C D B /
B

D / C /

56 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Which is better?
Graphs are often sparse
Streets form grids
Airlines rarely fly to all cities

Adjacency lists should generally be your default


choice
Slower performance compensated by greater
space savings

57 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Application: Moving Around WA State

What’s the shortest way to get from


Seattle to Pullman?

58 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Application: Moving Around WA State

What’s the fastest way to get from


Seattle to Pullman?

59 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Application: Reliability of Communication

If Wenatchee’s phone exchange goes down,


can Seattle still talk to Pullman?

60 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Application: Reliability of Communication

If Tacomas’s phone exchange goes down,


can Olympia still talk to Spokane?

61 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Applications: Bus Routes Downtown

If we’re at 3rd and Pine, how can we get to


1st and University using Metro?
How about 4th and Seneca?
62 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Graph Traversals
For an arbitrary graph and a starting node v,
find all nodes reachable from v (i.e., there exists
a path)
 Possibly "do something" for each node (print to
output, set some field, return from iterator, etc.)

Related Problems:
Is an undirected graph connected?
Is a digraph weakly/strongly connected?
For strongly, need a cycle back to starting node

63 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Graph Traversals
Basic Algorithm for Traversals:
Select a starting node
Make a set of nodes adjacent to current node
Visit each node in the set but "mark" each nodes
after visiting them so you don't revisit them (and
eventually stop)
Repeat above but skip "marked nodes"

64 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


IntraverseGraph(Node
Rough Code start) { Form
Set pending = emptySet();
pending.add(start)
mark start as visited
while(pending is not empty) {
next = pending.remove()
for each node u adjacent to next
if(u is not marked) {
mark u
pending.add(u)
}
}
}
}

65 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Running Time and Options
Assuming add and remove are O(1), entire
traversal is O(|E|) if using an adjacency list

The order we traverse depends entirely on how add


and remove work/are implemented
 DFS: a stack "depth-first graph search"
 BFS: a queue "breadth-first graph search"

DFS and BFS are "big ideas" in computer science


 Depth: recursively explore one part before going back
to the other parts not yet explored
66 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
 Breadth: Explore areas closer to start node first
Recursive DFS, Example with Tree
A treeA is a graph and DFS and BFS are particularly easy
to "see" in one DFS(Node start) {
mark and process start
B C for each node u adjacent to start
if u is not marked
D E F DFS(u)
}
G H

Order processed: A, B, D, E, C, F, G, H
 This is a "pre-order traversal" for trees
 The marking is unneeded here but because we

67
support
CSE arbitrary
332 Data Abstractions, graphs,
Summer 2012 we need a means to July
process
23, 2012
each node exactly once
Graph Traversals – Breadth First Search
 The idea in a breadth first search is opposite to a depth first
search.
 Instead of searching down a single path until you can go no longer, you
search all paths at an uniform depth from the source before moving onto
deeper paths. Once again, we'll need to mark both edges and vertices based
on what has been visited.

 In essence, we only want to explore one "unit" away from a


searched node before we move to a different node to search
from.
 All in all, we will be adding nodes to the back of a queue to be ones to
searched from in the future.

 In the implementation on the following page, a set of queues Li


are maintained, each storing a list of vertices a distance of i
edges from the starting vertex. One can implement this
algorithm with a single queue as well.
Breadth First Search Algorithm
BFS(G,s):
1) Let L0 be empty
2) Insert s into L0.
3) Let i = 0
4) While Li is not empty do the following:
A) Create an empty container Li+1.
B) For each vertex v in Li do
i) For all edges e incident to v
a) if e is unexplored, mark endpoint w.
b) if w is unexplored
Mark it.
Insert w into Li+1.
Label e as a discovery edge.
else
Label e as a cross edge.
C) i = i+1
Breadth First Search
The basic idea
we have successive rounds and continue with our rounds until
no new vertices are visited on a round.
For each round, we look at each vertex connected to the
vertex we came from.
 And from this vertex we look at all possible connected
vertices.
This leaves no vertex unvisited
 because we continue to look for vertices until no new ones of
a particular length are found.
If there are no paths of length 10 to a new vertex, surely there
can be no paths of length 11 to a new vertex.
The algorithm also terminates since no path can be longer
than the number of vertices in the graph.
Directed Graphs
Traversals
Both of the traversals DFS and BFS are
essentially the same on a directed graph.
When you run the algorithms, you must simply
pay attention to the direction of the edges.
Furthermore, you must keep in mind that you
will only visit edges that are reachable from the
source vertex.
Graph Traversal – Application
Mark and Sweep Algorithm for Garbage Collection
A mark bit is associated with each object created in a
Java program.
 It indicates if the object is live or not.
When the JVM notices that the memory heap is low, it
suspends all threads, and clears all mark bits.
 To garbage collect, we go through each live stack of current
threads and mark all these objects as live.
 Then we use a DFS to mark all objects reachable from these
initial live objects. (In particular each object is viewed as a
vertex and each reference as a directed edge.)
 This completes marking all live objects.
 Then we scan through the memory heap freeing all space
that has NOT been marked.
DFS with Stack, Example
DFS2(Node start) { with Tree
initialize stack s to hold start
A mark start as visited
while(s is not empty) {
B C next = s.pop() // and "process"
for each node u adjacent to next
D E F if(u is not marked)
mark u and push onto s
G H }
}

Order processed: A, C, F, H, G, B, E, D
A different order but still a perfectly fine traversal of the
graph
73 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
BFS with Queue, Example with Tree
BFS(Node start) {
initialize queue q to hold start
A mark start as visited
while(q is not empty) {
B C next = q.dequeue() // and "process"
for each node u adjacent to next
D E F if(u is not marked)
mark u and enqueue onto q
G H }
}

Order processed: A, B, C, D, E, F, G, H
A "level-order" traversal

74 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


DFS/BFS Comparison
BFS always finds the shortest path (or "optimal
solution") from the starting node to a target node
Storage for BFS can be extremely large
A k-nary tree of height h could result in a queue
size of kh

DFS can use less space in finding a path


If longest path in the graph is p and highest out-
degree is d then DFS stack never has more than
d⋅p elements

75 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Implications
For large graphs, DFS is hugely more memory
efficient, if we can limit the maximum path length to
some fixed d.

If we knew the distance from the start to the


goal in advance, we could simply not add any
children to stack after level d

But what if we don’t know d in advance?

76 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Iterative Deepening (IDFS)
Algorithms
Try DFS up to recursion of K levels deep.
If fails, increment K and start the entire search
over

Performance:
Like BFS, IDFS finds shortest paths
Like DFS, IDFS uses less space
Some work is repeated but minor compared to
space savings
77 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Saving the Path
Our graph traversals can answer the standard
reachability question:
"Is there a path from node x to node y?"

But what if we want to actually output the path?

Easy:
 Store the previous node along the path:
When processing u causes us to add v to the search,
set v.path field to be u)
 When you reach the goal, follow path fields back to
A Stack!!
where you started (and then reverse the answer)
CSE 332 Data Abstractions, Summer 2012 July 23, 2012
78  What's an easy way to do the reversal?
Example
What using
is a path from BFS
Seattle to Austin?
 Remember marked nodes are not re-enqueued
 Note shortest paths may not be unique

0 1 Chicago
Seattle

1 Salt Lake City

Austin

1
3
San Francisco
2
Dallas

79 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Topological Sort
Problem: Given a DAG G=(V, E), output all the
vertices in order such that if no vertex appears
CSE 331
before any other vertex that has an edge to it CSE 440
CSE 332

Example input:
CSE 142 CSE 143 CSE 311

CSE 312
MATH CSE 341
126

CSE 351 CSE 333

CSE 352

Example output: Disclaimer: Do not use for official advising purposes!


142, 126, 143, 311, (Implies that CSE 332 is a pre-req for CSE 312 – not true)
80
331, 332, 312, 341, 351,
CSE 332 Data Abstractions, Summer 2012
333,
July 23, 2012
440, 352
Questions and Comments
Terminology:
A DAG represents a partial order and a topological sort
produces a total order that is consistent with it

Why do we perform topological sorts only on DAGs?


 Because a cycle means there is no correct answer

Is there always a unique answer?


 No, there can be one or more answers depending on
the provided graph

What DAGs have exactly 1 answer?


81  Lists
CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Uses Topological Sort
Figuring out how to finish your degree

Computing the order in which to recalculate cells in


a spreadsheet

Determining the order to compile files with


dependencies

In general, use a dependency graph to find an


allowed order of execution

82 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Topological Sort: First Approach
1. Label each vertex with its in-degree
 Think "write in a field in the vertex"
 You could also do this with a data structure on the
side

2. While there are vertices not yet outputted:


a) Choose a vertex v labeled with in-degree of 0
b) Output v and "remove it" from the graph
c) For each vertex u adjacent to v, decrement in-
degree of u
- (i.e., u such that (v,u) is in E)

83 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Output:
Example
CSE 331 CSE 440

CSE 332

CSE 142 CSE 143 CSE 311

CSE 312
MATH CSE 341
126

CSE 351 CSE 333


CSE 352

Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed?
In-deg:

84 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Output:
Example
CSE 331 CSE 440

CSE 332

CSE 142 CSE 143 CSE 311

CSE 312
MATH CSE 341
126

CSE 351 CSE 333


CSE 352

Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed?
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1

85 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Output:
Example 126
CSE 331 CSE 440

CSE 332

CSE 142 CSE 143 CSE 311

CSE 312
MATH CSE 341
126

CSE 351 CSE 333


CSE 352

Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1
86 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Output:
Example 126
CSE 331 CSE 440
142

CSE 332

CSE 142 CSE 143 CSE 311

CSE 312
MATH CSE 341
126

CSE 351 CSE 333


CSE 352

Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1
0
CSE 332 Data Abstractions, Summer 2012
87 July 23, 2012
Output:
Example 126
CSE 331 CSE 440
142
143
CSE 332

CSE 142 CSE 143 CSE 311

CSE 312
MATH CSE 341
126

CSE 351 CSE 333


CSE 352

Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 0 0 0
0
CSE 332 Data Abstractions, Summer 2012
88 July 23, 2012
Output:
Example 126
CSE 331 CSE 440
142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311

CSE 312
MATH CSE 341
126

CSE 351 CSE 333


CSE 352

Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 0 0
0
CSE 332 Data Abstractions, Summer 2012
89 July 23, 2012
Output:
Example 126
CSE 331 CSE 440
142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
CSE 312
MATH CSE 341
126

CSE 351 CSE 333


CSE 352

Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 0 0
0
CSE 332 Data Abstractions, Summer 2012
90 July 23, 2012
Output:
Example 126
CSE 331 CSE 440
142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
CSE 312 332
MATH CSE 341
126

CSE 351 CSE 333


CSE 352

Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0
0 0
CSE 332 Data Abstractions, Summer 2012
91 July 23, 2012
Output:
Example 126
CSE 331 CSE 440
142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
CSE 312 332
MATH CSE 341
126 312
CSE 351 CSE 333
CSE 352

Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0
0 0
CSE 332 Data Abstractions, Summer 2012
92 July 23, 2012
Output:
Example 126
CSE 331 CSE 440
142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
CSE 312 332
MATH CSE 341
126 312
341
CSE 351 CSE 333
CSE 352

Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0
0 0
CSE 332 Data Abstractions, Summer 2012
93 July 23, 2012
Output:
Example 126
CSE 331 CSE 440
142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
CSE 312 332
MATH CSE 341
126 312
341
CSE 351 CSE 333
351
CSE 352

Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0 0
0 0
CSE 332 Data Abstractions, Summer 2012
0
94 July 23, 2012
Output:
Example 126
CSE 331 CSE 440
142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
CSE 312 332
MATH CSE 341
126 312
341
CSE 351 CSE 333
351
CSE 352
333
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0 0
0 0
CSE 332 Data Abstractions, Summer 2012
0
95 July 23, 2012
Output:
Example 126 352
CSE 331 CSE 440
142
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
CSE 312 332
MATH CSE 341
126 312
341
CSE 351 CSE 333
351
CSE 352
333
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0 0
0 0
CSE 332 Data Abstractions, Summer 2012
0
96 July 23, 2012
Output:
Example 126 352
CSE 331 CSE 440
142 440
143
CSE 332
… 311
CSE 142 CSE 143 CSE 311
331
CSE 312 332
MATH CSE 341
126 312
341
CSE 351 CSE 333
351
CSE 352
333
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed? x x x x x x x x x x x x
In-deg: 0 0 2 1 2 1 1 2 1 1 1 1
1 0 1 0 0 1 0 0 0 0
0 0
CSE 332 Data Abstractions, Summer 2012
0
97 July 23, 2012
Running Time?
labelEachVertexWithItsInDegree();
for(i=0; i < numVertices; i++) {
v = findNewVertexOfDegreeZero();
put v next in output
for each w adjacent to v
w.indegree--;
}

What is the worst-case running time?


 Initialization O(|V| + |E|) (assuming adjacency list)
 Sum of all find-new-vertex O(|V|2) (because each O(|V|))
 Sum of all decrements O(|E|) (assuming adjacency list)
 So total is O(|V|2 + |E|) – not good for a sparse graph!

98 CSE 332 Data Abstractions, Summer 2012 July 23, 2012


Doing Better
Avoid searching for a zero-degree node every time!
 Keep the “pending” zero-degree nodes in a list, stack, queue,
bag, or something that gives O(1) add/remove
 Order we process them affects the output but not
correctness or efficiency

Using a queue:
 Label each vertex with its in-degree,
 Enqueue all 0-degree nodes
 While queue is not empty
 v = dequeue()
 Output v and remove it from the graph
 For each vertex u adjacent to v, decrement the in-degree of u
99 and
CSE 332 if Abstractions,
Data new degree is 0,2012
Summer enqueue it July 23, 2012
Running Time?
labelAllWithIndegreesAndEnqueueZeros();

for(i=0; i < numVertices; i++) {


v = dequeue();
put v next in output
for each w adjacent to v {
w.indegree--;
if(w.indegree==0)
enqueue(w);
}
}
 Initialization: O(|V| + |E|) (assuming adjacency list)
 Sum of all enqueues and dequeues: O(|V|)
 Sum of all decrements: O(|E|) (assuming adjacency list)
 So total is O(|E| + |V|) – much better for sparse graph!

100 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
More Graph Algorithms
Finding a shortest path is one thing
What happens when we consider weighted edges
(as in distances)?

Next time we will discuss shortest path algorithms


and the contributions of a curmudgeonly computer
scientist

101 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Thank you

102 CSL 201 Data Structures (ODD Semester) 12/29/20

You might also like