Graph: Dr. Krishan Kumar Assistant Professor & Head Computer Science & Engineering
Graph: Dr. Krishan Kumar Assistant Professor & Head Computer Science & Engineering
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.
2 edges here
More Notation A
C
For a graph G = (V, E):
|V| is the number of vertices B
More Notation A
C
For a graph G = (V, E):
|V| is the number of vertices B
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?
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]
B
Is there a path from A to D? No
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
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
D
A B /
A
C
B A /
B
C D B /
D /
Delete an edge:
Space requirements:
A B /
D
A B C / A /
C
C D B /
B
D / C /
Related Problems:
Is an undirected graph connected?
Is a digraph weakly/strongly connected?
For strongly, need a cycle back to starting node
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.
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
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?"
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
Austin
1
3
San Francisco
2
Dallas
CSE 312
MATH CSE 341
126
CSE 352
CSE 332
…
CSE 142 CSE 143 CSE 311
CSE 312
MATH CSE 341
126
Node: 126 142 143 311 312 331 332 333 341 351 352 440
Removed?
In-deg:
CSE 332
…
CSE 142 CSE 143 CSE 311
CSE 312
MATH CSE 341
126
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
CSE 332
…
CSE 142 CSE 143 CSE 311
CSE 312
MATH CSE 341
126
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
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
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
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
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
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--;
}
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();
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)?
101 CSE 332 Data Abstractions, Summer 2012 July 23, 2012
Thank you