0% found this document useful (0 votes)
24 views73 pages

Graphs

Graphs are composed of nodes connected by edges. They can represent many types of relationships and are more general than other data structures. A graph is formally defined as a pair (V,E) where V is a set of vertices and E is a set of edges connecting the vertices. Graphs can be directed or undirected. Key terminology includes vertices, edges, paths, degrees, and adjacency.

Uploaded by

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

Graphs

Graphs are composed of nodes connected by edges. They can represent many types of relationships and are more general than other data structures. A graph is formally defined as a pair (V,E) where V is a set of vertices and E is a set of edges connecting the vertices. Graphs can be directed or undirected. Key terminology includes vertices, edges, paths, degrees, and adjacency.

Uploaded by

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

Graphs

COL 106
Slide Courtesy : http://courses.cs.washington.edu/courses/cse373/
Douglas W. Harder, U Waterloo
What are graphs?

• Yes, this is a graph….

• But we are interested in a different kind of “graph”

12/26/03 Graph Terminology - Lecture 13 2


Graphs

• Graphs are composed of


– Nodes (vertices)
– Edges (arcs) node

edge

12/26/03 Graph Terminology - Lecture 13 3


Varieties
• Nodes
– Labeled or unlabeled
• Edges
– Directed or undirected
– Labeled or unlabeled

12/26/03 Graph Terminology - Lecture 13 4


Motivation for Graphs
node node
• Consider the data structures we have
looked at so far… Value Next Value Next

• Linked list: nodes with 1 incoming edge +


1 outgoing edge 94

• Binary trees/heaps: nodes with 1


incoming edge + 2 outgoing edges
10 97
• B-trees: nodes with 1 incoming edge +
multiple outgoing edges 3 5
96 99

12/26/03 Graph Terminology - Lecture 13 5


Motivation for Graphs

• How can you generalize these data structures?


• Consider data structures for representing the
following problems…

12/26/03 Graph Terminology - Lecture 13 6


CSE Course Prerequisites
461
322
373 106
321

100 326
410 415 370 341

413 417
378
421
Nodes = courses
Directed edge = prerequisite 401

12/26/03 Graph Terminology - Lecture 13 7


Representing a Maze

S
S

E
E

Nodes = rooms
Edge = door or passage

12/26/03 Graph Terminology - Lecture 13 8


Representing Electrical Circuits
Battery Switch

Nodes = battery, switch, resistor, etc. Resistor


Edges = connections

12/26/03 Graph Terminology - Lecture 13 9


Program statements

x1 x2

x1=q+y*z + -
x2=y*z-q
Naive:
q * * q

y*z calculated twice y z

x1 x2
common
subexpression + -
eliminated:
q * q
Nodes = symbols/operators y z
Edges = relationships

12/26/03 Graph Terminology - Lecture 13 10


Precedence

S1 a=0; 6
S2 b=1;
S3 c=a+1
5
S4 d=b+a;
S5 e=d+1;
S6 e=c+d;

4
Which statements must execute before S6? 3
S1, S2, S3, S4

Nodes = statements
Edges = precedence requirements 1 2

12/26/03 Graph Terminology - Lecture 13 11


Information Transmission in a Computer
Network
56
Delhi Seattle
Mumbai 128

16 181 New York


30 140

L.A.
Sydney
Nodes = computers
Edges = transmission rates

12/26/03 Graph Terminology - Lecture 13 12


Traffic Flow on Highways

UW
Nodes = cities
Edges = # vehicles on
connecting highway

12/26/03 Graph Terminology - Lecture 13 13


Graph Definition
• A graph is simply a collection of nodes plus edges
– Linked lists, trees, and heaps are all special cases of graphs
• The nodes are known as vertices (node = “vertex”)
• Formal Definition: A graph G is a pair (V, E) where
– V is a set of vertices or nodes
– E is a set of edges that connect vertices

12/26/03 Graph Terminology - Lecture 13 14


Graph Example

• Here is a directed graph G = (V, E)


– Each edge is a pair (v1, v2), where v1, v2 are vertices in V
– V = {A, B, C, D, E, F}
E = {(A,B), (A,D), (B,C), (C,D), (C,E), (D,E)}

B
C
A
F
D E
12/26/03 Graph Terminology - Lecture 13 15
Directed vs Undirected Graphs
• If the order of edge pairs (v1, v2) matters, the graph is directed
(also called a digraph): (v1, v2) ≠(v2, v1)

v1 v2

• If the order of edge pairs (v1, v2) does not matter, the graph is
called an undirected graph: in this case, (v1, v2) = (v2, v1)

v1 v2

12/26/03 Graph Terminology - Lecture 13 16


Undirected Terminology
• Two vertices u and v are adjacent in an undirected graph
G if {u,v} is an edge in G
• edge e = {u,v} is incident with vertex u and vertex v

• A graph is connected if given any two vertices u and v,


there is a path from u to v

• The degree of a vertex in an undirected graph is the


number of edges incident with it
– a self-loop counts twice (both ends count)
– denoted with deg(v)

12/26/03 Graph Terminology - Lecture 13 17


Undirected Terminology

B is adjacent to C and C is adjacent to B


(A,B) is incident
to A and to B B
Self-loop
C
A
F
D E Degree = 0
Degree = 3

12/26/03 Graph Terminology - Lecture 13 18


Directed Terminology

• Vertex u is adjacent to vertex v in a directed graph


G if (u,v) is an edge in G
– vertex u is the initial vertex of (u,v)
• Vertex v is adjacent from vertex u
– vertex v is the terminal (or end) vertex of (u,v)
• Degree
– in-degree is the number of edges with the vertex as
the terminal vertex
– out-degree is the number of edges with the vertex as
the initial vertex

12/26/03 Graph Terminology - Lecture 13 19


Directed Terminology

B adjacent to C and C adjacent from B

B
C
A
F
D E In-degree = 0
Out-degree = 0
In-degree = 2
Out-degree = 1

12/26/03 Graph Terminology - Lecture 13 20


More Graph Terminology
• simple path: no repeated vertices a b

bec
c

d e
• cycle: simple path, except that the last vertex is the same as the first
vertex a b

acda
c

d e
Graphs 21
Even More Terminology
•connected graph: any two vertices are connected by some path

connected not connected


• subgraph: subset of vertices and edges forming a graph
• connected component: maximal connected subgraph. E.g., the
graph below has 3 connected components.

Graphs 22
Trees from Perspective of Graphs
• (free) tree - connected graph without cycles
• forest - collection of trees
tree

tree

forest

tree

tree

Graphs 23
Handshaking Theorem
• Let G=(V,E) be an undirected graph with |E|=e edges.
Then

2e   deg(v) Add up the degrees of all vertices.


vV

• Every edge contributes +1 to the degree of each of


the two vertices it is incident with
– number of edges is exactly half the sum of deg(v)
– the sum of the deg(v) values must be even

12/26/03 Graph Terminology - Lecture 13 24


Connectivity
• Let n = #vertices, and m = #edges
• A complete graph: one in which all pairs of vertices are adjacent
• How many total edges in a complete graph?
– Each of the n vertices is incident to n-1 edges, however, we would have
counted each edge twice!!! Therefore, intuitively, m = n(n -1)/2.
• Therefore, if a graph is not complete, m < n(n -1)/2

n 5
m  (5 

Graphs 25
Spanning Tree
• A spanning tree of G is a subgraph which is a tree and which
contains all vertices of G

G spanning tree of G

• Failure on any edge disconnects system (least fault tolerant)


Graphs 26
Graph Representations

• Space and time are analyzed in terms of:


• Number of vertices = |V| and
• Number of edges = |E|
• There are at least two ways of representing
graphs:
• The adjacency matrix representation
• The adjacency list representation
12/26/03 Graph Terminology - Lecture 13 27
Adjacency Matrix
A B C D E F
B
C A 0 1 0 1 0 0
A
B 1 0 1 0 0 0
F
C 0 1 0 1 1 0
D E
D 1 0 1 0 1 0
E 0 0 1 1 0 0
1 if (v, w) is in E
M(v, w) =
0 otherwise F 0 0 0 0 0 0
Space = |V|2
12/26/03 Graph Terminology - Lecture 13 28
Adjacency Matrix for a Digraph
A B C D E F
B
C A 0 1 0 1 0 0
A
B 0 0 1 0 0 0
F
C 0 0 0 1 1 0
D E
D 0 0 0 0 1 0
E 0 0 0 0 0 0
1 if (v, w) is in E
M(v, w) =
0 otherwise F 0 0 0 0 0 0
Space = |V|2
12/26/03 Graph Terminology - Lecture 13 29
Adjacency List
For each v in V, L(v) = list of w such that (v, w) is in E
a b

B A B D
list of
neighbors
C B
A A C

F C B D E
D E D A C E
E C D
F
Space = a |V| + 2 b |E|
12/26/03 Graph Terminology - Lecture 13 30
Adjacency List for a Digraph
For each v in V, L(v) = list of w such that (v, w) is in E
a b

B A B D
C B
A C

F C D E
D E D E
E
F
Space = a |V| + b |E|
12/26/03 Graph Terminology - Lecture 13 31
Searching in graphs
• Find Properties of Graphs
– Spanning trees
– Connected components
– Bipartite structure
– Biconnected components
• Applications
– Finding the web graph – used by Google and others
– Garbage collection – used in Java run time system

12/26/03 Graph Searching - Lecture 16 32


Graph Searching Methodology
Breadth-First Search (BFS)
• Breadth-First Search (BFS)
– Use a queue to explore neighbors of source
vertex, then neighbors of neighbors etc.
– All nodes at a given distance (in number of edges)
are explored before we go further

12/26/03 Graph Searching - Lecture 16 33


Breadth-First Search
• A Breadth-First Search (BFS) traverses a connected component of a
graph, and in doing so defines a spanning tree with several useful
properties.
• The starting vertex s has level 0, and defines that point as an
“anchor.”
• In the first round, all of the nodes that are only one edge away from
the anchor are visited.
• These nodes are placed into level 1
• In the second round, all the new nodes that can be reached by one
edge from level 1 nodes are visited and placed in level 2.
• This continues until every vertex has been assigned a level.
• The label of any vertex v corresponds to the length of the shortest
path from s to v.
34
Example
Consider the graph from previous example
Example
Performing a breadth-first traversal
– Push the first vertex onto the queue

A
Example
Performing a breadth-first traversal
– Pop A and push B, C and E
A

B C E
Example
Performing a breadth-first traversal:
– Pop B and push D
A, B

C E D
Example
Performing a breadth-first traversal:
– Pop C and push F
A, B, C

E D F
Example
Performing a breadth-first traversal:
– Pop E and push G and H
A, B, C, E

D F G H
Example
Performing a breadth-first traversal:
– Pop D
A, B, C, E, D

F G H
Example
Performing a breadth-first traversal:
– Pop F
A, B, C, E, D, F

G H
Example
Performing a breadth-first traversal:
– Pop G and push I
A, B, C, E, D, F, G

H I
Example
Performing a breadth-first traversal:
– Pop H
A, B, C, E, D, F, G, H

I
Example
Performing a breadth-first traversal:
– Pop I
A, B, C, E, D, F, G, H, I
Example
Performing a breadth-first traversal:
– The queue is empty: we are finished
A, B, C, E, D, F, G, H, I
Another Example
0 0 1

A B C D A B C D

a) b)
E F G H E F G H

I J K L I J K L

M N O P M N O P
0 1 2
0 1 2 3
A B C D B C D
A
c) d)
E F G H E F G H

I J K L
I J K L

M N O P
47
M N O P
Example Continued
0 1 2 3 0 1 2 3

A B C D A B C D

E F G H E F G H

4 4
I J K L I J K L

M N O P M N O P 5

Generic DFS and BFS 48


Breadth-First Search

BFS
Initialize Q to be empty;
Enqueue(Q,1) and mark 1;
while Q is not empty do
i := Dequeue(Q);
for each j adjacent to i do
if j is not marked then
Enqueue(Q,j) and mark j;
end{BFS}

12/26/03 Graph Searching - Lecture 16 49


BFS Pseudo-Code
Algorithm BFS(s): Input: A vertex s in a graph
Output: A labeling of the edges as “discovery” edges and “cross edges”
initialize container L0 to contain vertex s
i0
while Li is not empty do
create container Li+1 to initially be empty
for each vertex v in Li do
if edge e incident on v do
let w be the other endpoint of e
if vertex w is unexplored then
label e as a discovery edge
insert w into Li+1
else label e as a cross edge
ii+1

50
Properties of BFS
• Proposition: Let G be an undirected graph on which a a BFS traversal starting at
vertex s has been performed. Then
– The traversal visits all vertices in the connected component of s .
– The discovery-edges form a spanning tree T, which we call the BFS tree, of the
connected component of s
– For each vertex v at level i, the path of the BFS tree T between s and v has i edges, and
any other path of G between s and v has at least i edges.
– If (u, v) is an edge that is not in the BFS tree, then the level numbers of u and v differ by
at most one.
• Proposition: Let G be a graph with n vertices and m edges. A BFS traversal of G
takes time O(n + m). Also, there exist O(n + m) time algorithms based on BFS for
the following problems:
– Testing whether G is connected.
– Computing a spanning tree of G
– Computing the connected components of G
– Computing, for every vertex v of G, the minimum number of edges of any path between
s and v. 52
BFS Properties
• Proposition: Let G be an undirected graph on which a BFS traversal
starting at a vertex s has been preformed. Then:
1. The traversal visits all vertices in the connected component of s
2. The discovery edges form a spanning tree of the connected component of s

• Justification of 1:
– Let’s use a contradiction argument: suppose there is at least on vertex v not visited and let
w be the first unvisited vertex on some path from s to v.
– Because w was the first unvisited vertex on the path, there is a neighbor u that has been
visited.
– But when we visited u we must have looked at edge(u, w). Therefore w must have been
visited.

• Justification of 2:
– We only mark edges from when we go to unvisited vertices. So we never form a cycle of
discovery edges, i.e. discovery edges form a tree.
– This is a spanning tree because BFS visits each vertex in the connected component of s

53
Graph Searching Methodology Depth-
First Search (DFS)
• Depth-First Search (DFS)
– Searches down one path as deep as possible
– When no nodes available, it backtracks
– When backtracking, it explores side-paths that
were not taken
– Uses a stack (instead of a queue in BFS)
– Allows an easy recursive implementation

12/26/03 Graph Searching - Lecture 16 54


Depth First Search Algorithm
• Recursive marking algorithm
• Initially every vertex is unmarked DFS(i)
i

DFS(i: vertex)
mark i; DFS(j)
j
for each j adjacent to i do
if j is unmarked then DFS(j)
end{DFS} k

Marks all vertices reachable from i

12/26/03 Graph Searching - Lecture 16 55


Depth-First Search
Algorithm DFS(v); Input: A vertex v in a graph
Output: A labeling of the edges as “discovery” edges and
“backedges”
for each edge e incident on v do
if edge e is unexplored then let w be the other endpoint of e
if vertex w is unexplored then label e as a discovery edge
recursively call DFS(w)
else label e as a backedge

DFS 56
DFS Application: Spanning Tree
• Given a (undirected) connected graph G(V,E) a
spanning tree of G is a graph G’(V’,E’)
– V’ = V, the tree touches all vertices (spans) the
graph
– E’ is a subset of E such that G’ is connected and
there is no cycle in G’

12/26/03 Graph Searching - Lecture 16 57


Example of DFS: Graph connectivity
and spanning tree
2
1 DFS(1)
7

5
6
4

12/26/03 Graph Searching - Lecture 16 58


Example Step 2
2
DFS(1)
1 7 DFS(2)

5
6
4

Red links will define the spanning tree if the graph


is connected

12/26/03 Graph Searching - Lecture 16 59


Example Step 5
2
DFS(1)
1 7 DFS(2)
DFS(3)
3 DFS(4)
5 DFS(5)

6
4

12/26/03 Graph Searching - Lecture 16 60


Example Steps 6 and 7
2
DFS(1)
1 7 DFS(2)
DFS(3)
3 DFS(4)
DFS(5)
5
DFS(3)
6 DFS(7)
4

12/26/03 Graph Searching - Lecture 16 61


Example Steps 8 and 9
2
DFS(1)
1 7 DFS(2)
DFS(3)
3 DFS(4)
DFS(5)
5
DFS(7)
6
Now back up.
4

12/26/03 Graph Searching - Lecture 16 62


Example Step 10 (backtrack)
2
DFS(1)
1 7 DFS(2)
DFS(3)
3 DFS(4)
5 DFS(5)

6 Back to 5,
but it has no
more neighbors.
4

12/26/03 Graph Searching - Lecture 16 63


Example Step 12
2
DFS(1)
1 7 DFS(2)
DFS(3)
3 DFS(4)
5 DFS(6)

6 Back up to 4.
From 4 we can
4 get to 6.

12/26/03 Graph Searching - Lecture 16 64


Example Step 13
2
DFS(1)
1 7 DFS(2)
DFS(3)
3 DFS(4)
5 DFS(6)

6 From 6 there is
nowhere new
to go. Back up.
4

12/26/03 Graph Searching - Lecture 16 65


Example Step 14
2
DFS(1)
1 7 DFS(2)
DFS(3)
3 DFS(4)
5
Back to 4.
6 Keep backing up.

12/26/03 Graph Searching - Lecture 16 66


Example Step 17
2
DFS(1)
1 7
All the way
3 back to 1.

5 Done.

6
4
All nodes are marked so graph is connected; red links
define a spanning tree

12/26/03 Graph Searching - Lecture 16 67


Finding Connected Components using DFS

2
1 3 7
10 4
9
5
6
8
11

3 connected components

12/26/03 Graph Searching - Lecture 16 68


Connected Components

2
1 3 7
10 4
9
5
6
8
11

3 connected components are labeled

12/26/03 Graph Searching - Lecture 16 69


Running Time Analysis
• Remember:
-DFS is called on each vertex exactly once.
-Every edge is examined exactly twice, once from each of its vertices
• For ns vertices and ms edges in the connected component of the
vertex s, a DFS starting at s runs in O(ns +ms) time if the graph is
represented in a data structure, like the adjacency list, where vertex
and edge methods take constant time.
• Marking a vertex as explored, and testing to see if a vertex has been
explored, takes O(degree)
• By marking visited nodes, we can systematically consider the edges
incident on the current vertex so we do not examine the same edge
more than once.

DFS 70
Marking Vertices
• Let’s look at ways to mark vertices in a way that satisfies the above
condition.
• Extend vertex positions to store a variable for marking

Before After
Position Position

Element Element isMarked

Use a hash table mechanism which satisfies the above condition is the probabilistic sense,
because is supports the mark and test operations in O(1) expected time

DFS 71
Performance DFS
• n vertices and m edges
• Storage complexity O(n + m)
• Time complexity O(n + m)
• Linear Time!

12/26/03 Graph Searching - Lecture 16 72


DFS Properties
• Proposition: Let G be an undirected graph on which a DFS traversal
starting at a vertex s has been preformed. Then:
1. The traversal visits all vertices in the connected component of s
2. The discovery edges form a spanning tree of the connected component of s

• Justification of 1:
– Let’s use a contradiction argument: suppose there is at least on vertex v not visited and let
w be the first unvisited vertex on some path from s to v.
– Because w was the first unvisited vertex on the path, there is a neighbor u that has been
visited.
– But when we visited u we must have looked at edge(u, w). Therefore w must have been
visited.

• Justification of 2:
– We only mark edges from when we go to unvisited vertices. So we never form a cycle of
discovery edges, i.e. discovery edges form a tree.
– This is a spanning tree because DFS visits each vertex in the connected component of s

84
Depth-First vs Breadth-First
• Depth-First
– Stack or recursion
– Many applications
• Breadth-First
– Queue (recursion no help)
– Can be used to find shortest paths from the start vertex

12/26/03 Graph Searching - Lecture 16 85

You might also like