0% found this document useful (0 votes)
17 views16 pages

Graph Theory

The document provides an overview of graph terminology, including definitions of graphs, vertices, edges, paths, cycles, and various types of graphs such as directed, undirected, weighted, and complete graphs. It also explains concepts like adjacency, loops, and degrees of vertices, along with applications of graphs in communication networks and driving distance maps. Additionally, it introduces graph search methods, specifically the Breadth-First Search algorithm, detailing its process and implementation.

Uploaded by

Ayush Shahu
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)
17 views16 pages

Graph Theory

The document provides an overview of graph terminology, including definitions of graphs, vertices, edges, paths, cycles, and various types of graphs such as directed, undirected, weighted, and complete graphs. It also explains concepts like adjacency, loops, and degrees of vertices, along with applications of graphs in communication networks and driving distance maps. Additionally, it introduces graph search methods, specifically the Breadth-First Search algorithm, detailing its process and implementation.

Uploaded by

Ayush Shahu
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

Graph Terminology

Introduction to Algorithms
„ A graph G = (V, E)
‰ V = set of vertices
‰ E = set of edges

„ In an undirected graph:
‰ edge(u, v) = edge(v, u)

„ In a directed graph:
‰ edge(u,v) goes from vertex u to vertex v, notated u →
v
Chapter 22: Elementary Graph ‰ edge(u, v) is not the same as edge(v, u)
Algorithms
2

Graph Terminology Graph Terminology


A A
„ Adjacent vertices: connected by an edge
‰ Vertex v is adjacent to u if and only if (u, v) ∈ E.
B C B C
‰ I an undirected
In di t d graph h with d (u, v),
ith edge ) andd
hence (v, u), v is adjacent to u and u is adjacent
to v.
D D
a b a b

Directed graph: Undirected graph: c c

d e d e
V = {A, B, C, D} V = {A, B, C, D}
E = {(A,B), (A,C), (A,D), (C,B)} E = {(A,B), (A,C), (A,D), (C,B), Vertex a is adjacent to c and Vertex c is adjacent to a, but
(B,A), (C,A), (D,A), (B,C)} vertex c is adjacent to a vertex a is NOT adjacent to c

3 4
Graph Terminology Graph Terminology
„ Loops
„ A Path in a graph from u to v is a sequence of edges
b
between vertices
i w0, w1, …, wk, such h (wi, wi+1) ∈ E,
h that ‰ If the graph contains an edge (v, v) from a vertex to itself,
itself
then the path v, v is sometimes referred to as a loop.
u = w0 and v = wk, for 0 ≤ i < k a b
‰ The length of the path is k, the number of edges on
the path c

d e
a b a b

‰ The graphs we will consider will generally be loopless.


c c

d e d e „ A simple path is a path such that all vertices are distinct, except that the first
and last could be the same.
abedce is a path. acde is a path.
cdeb is a path.
path abec
b isi NOT a path.
th a b
bca is NOT a path.
c abedc is a simple path.
cdec is a simple path.
d e abedce is NOT a simple path.

5 6

Graph Terminology Graph


p Terminology
gy
„ Cycles
‰ A cycle in a directed graph is a path of length at least 2
„ simple path: no repeated vertices such that the first vertex on the path is the same as the
last one; if the path is simple, then the cycle is a simple
cycle.
a b
abeda is a simple cycle.
c abeceda is a cycle, but is NOT a simple cycle.
abedc is NOT a cycle.
cycle
d e

‰ A cycle in a undirected graph


„ A path
th off length
l th att least
l t 3 such
h th
thatt th
the first
fi t vertex
t on the
th path
th
is the same as the last one.
„ The edges on the path are distinct.
a b aba is NOT a cycle.
abedceda is NOT a cycle.
c abedcea is a cycle, but NOT simple.
abea is a simple cycle.
cycle
d e

7 8
Graph
p Terminology
gy Graph Terminology
„ If each edge in the graph carries a value, then the graph is
called weighted graph.
„ Complete Undirected Graph
‰ A weighted graph is a graph G = (V, E, W), where each ‰ has all possible edges
edge, e ∈ E is assigned a real valued weight, W(e).
„ A complete graph is a graph with an edge between every
pair of vertices.
‰ A graph ll d complete
h iis called l t graphh if every vertex
t isi
adjacent to every other vertex.

n=1 n=2 n=3 n=4

10

Graph
p Terminology
gy Graph Terminology
„ connected graph: any two „ tree - connected graph without cycles
vertices are connected by
some path
‰ An undirected graph is
„ forest - collection of trees
connected if, for every
pair of vertices u and v
p
there is a path from u to
v.

12
Graph Terminology Graph
p Terminology
gy
„ End vertices (or endpoints) of an edge a
‰ U and V are the endpoints
p of a

„ Edges incident on a vertex V


‰ a, d, and b are incident on V V
a b
h j
„ Adjacent vertices
‰ U and V are adjacent U d X Z
c e i
„ Degree of a vertex X
‰ X has degree 5 W g
„ Parallel edges f
‰ h and d i are parallel
ll l edges
d Y

„ Self-loop
‰ j is a self-loop

13 14

Graph
p Terminology
gy Graph
p Terminology
gy
„ Path „ Cycle
‰ sequence of alternating ‰ circular sequence of
vertices and edges alternating vertices and edges
‰ begins with a vertex
ends with a vertex
‰
V „ Simple cycle V
a b ‰ cycle such that all its vertices
a b
„ Simple path P1 and edges are distinct
‰ path such that all its vertices U d X Z U d X Z
and edges are distinct. P2 h Examples C2 h
„ Examples c e
„
e C1
‰ C1 = (V, X, Y, W, U, V) is a c
‰ P1 = (V,
(V XX, Z) is
i a simple
i l path.th W g simple cycle W g
‰ P2 = (U, W, X, Y, W, V) is a
‰ C2 = (U, W, X, Y, W, V, U) is a
path that is not simple. f cycle that is not simple f
Y Y

15 16
In-Degree of a Vertex Out-Degree of a Vertex
„ in degree is number of incoming edges
in-degree „ out degree is number of outbound edges
out-degree
‰ indegree(2) = 1, indegree(8) = 0 ‰ outdegree(2) = 1, outdegree(8) = 2
2 2
3 3
8 8
1 10 1 10
4 4
5 5
9 9
11 11

6 6
7 7
17 18

Applications:
pp Communication Driving Distance/Time Map
Network „ vertex
e e = ccity,
y,
„ vertex = city, edge = communication link
„ edge weight = distance/time
2
3 2
4 3
8
1 8 8
10
1 6 10
4 2 4 5
5 4 4 3
9 5
11 9
11
5 6

6
7 6 7
7

19 20
Street Map
p Computer Networks
„ Some streets are one way
„ A bidirectional link represented
p by
y 2 directed „ Electronic
ect o c circuits
c cu ts cslab1a cslab1b

edge ‰ Printed circuit board


math.brown.edu
‰ (5, 9) (9, 5)
2 cs.brown.edu
3 „ Computer networks
8 ‰ Local area network
brown.edu
1 10 ‰ Internet qwest.net
att.net
4 ‰ Web
5
9
11
cox.net
John

Paul
6 David
7

21 22

Graphs Graph Search Methods


„ We will typically
y y express running
g times in „ Many graph problems solved using a search
terms of method
‰ |V| = number of vertices, and ƒ Path from one vertex to another
‰ |E| = number of edges ƒ Is the graph connected?
‰ If |E| ≈ |V|2 the graph is dense ƒ etc.
‰ If |E| ≈ |V| the graph is sparse

„ Commonly used search methods:


„ If y
you know yyou are dealing
g with dense or ƒ Breadth-first
Breadth first search
sparse graphs, different data structures may ƒ Depth-first search
make sense
23 24
Graph Search Methods Breadth-First Search
„ A vertex u is reachable from vertex v iff there is „ Visit start vertex (s) and put into a FIFO
a path from v to u.
„ A search method starts at a given vertex v and
queue.
visits everyy vertex that is reachable from v.
2
3
„ Repeatedly remove a vertex from the queue,
8 visit its unvisited adjacent vertices,
vertices put newly
1
visited vertices into the queue.
4
5
9
10 „ All vertices reachable from the start vertex
(s) (including the start vertex) are visited.
6
7 11
25 26

Breadth-First Search Breadth-First Search


BFS(G, s) {
„ Again
g will associate vertex “colors” to g
guide 1
// initialize vertices;
for each u ∈ V(G) – {s}{
the algorithm 2
3
do color[u] = WHITE
d[u] = ∞ // distance from s to u
4 p[u] = NIL // predecessor or parent of u
‰ White vertices have not been discovered }
5 color[s] = GREEN
„ All vertices start out white 6 d[s] = 0
7 p[s] = NIL
‰ Green vertices are discovered but not fully 8 Q = Empty;
9 Enqueue (Q,s); // Q is a queue; initialize to s
explored 10 while (Q not empty) {
11 u = Dequeue(Q);
„ They
y may
y be adjacent
j to white vertices 12 for each v ∈ adj[u] {
13 if (color[v] == WHITE)
‰ Black vertices are discovered and fully explored 14 color[v] = GREEN; What does d[v] represent?
15 d[v] = d[u] + 1;
„ They are adjacent only to black and green vertices 16 p[v] = u; What does p[
p[v]] represent?
p
17 Enqueue(Q, v);
„ Explore vertices by scanning adjacency list of }
18 color[u] = BLACK;
green vertices }
}

27 28
Breadth-First Search Breadth-First Search
„ Prior to the first iteration in line 10, the only green vertex,
„ Lines 1 1-4
4 paint every vertex white, set d[u] to be and the only vertex in Q Q, is the source vertex (s)
(s).
infinity for each vertex (u), and set p[u] the
parent of every vertex to be NIL. „ Line 11 determines the green vertex (u) at the head of the
queue Q and removes it from Q.
q
„ Line 5 paints the source vertex (s) green green.
„ Line 6 initializes d[s] to 0. „ The for loop of lines 12-17 considers each vertex (v) in the
adjacency list of (u).
„ Line 7 sets the parent of the source to be NIL.
„ If (v) is white, then it has not yet been discovered, and the
„ Lines 8-9 initialize Q to the queue containing just algorithm discovers it by executing lines 14-17.
the vertex (s).
‰ It is first greened
greened, and its distance d[v] is set to d[u]+1
d[u]+1.
„ Th while
The hil loop
l off lines
li 10 18 iterates
10-18 it t as longl as
there remain green vertices, which are ‰ Then, u is recorded as its parent.
discovered vertices that have not y yet had their ‰ Finally,
Finally it is placed at the tail of the queue QQ.
adjacency
dj lilists
t ffully
ll examined.
i d
‰ This while loop maintains the test in line 10, the queue „ When all the vertices on (u’s) adjacency list have been
Q consists of the set of the green vertices. examined, u is blackened in line 18.

29 30

Breadth-First Search: Example Breadth-First Search: Example


r s t u r s t u

∞ ∞ ∞ ∞ ∞ 0 ∞ ∞

∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞
v w x y v w x y

Q: s

31 32
Breadth-First Search: Example Breadth-First Search: Example
r s t u r s t u

1 0 ∞ ∞ 1 0 2 ∞

∞ 1 ∞ ∞ ∞ 1 2 ∞
v w x y v w x y

Q: w r Q: r t x

33 34

Breadth-First Search: Example Breadth-First Search: Example


r s t u r s t u

1 0 2 ∞ 1 0 2 3

2 1 2 ∞ 2 1 2 ∞
v w x y v w x y

Q: t x v Q: x v u

35 36
Breadth-First Search: Example Breadth-First Search: Example
r s t u r s t u

1 0 2 3 1 0 2 3

2 1 2 3 2 1 2 3
v w x y v w x y

Q: v u y Q: u y

37 38

Breadth-First Search: Example Breadth-First Search: Example


r s t u r s t u

1 0 2 3 1 0 2 3

2 1 2 3 2 1 2 3
v w x y v w x y

Q: y Q: Ø

39 40
Depth-First Search Depth-First Search
„ Depth-first
ep s search
sea c iss another
a o e sstrategy
a egy for
o „ Initialize
‰ color all vertices white
exploring a graph
„ Visit each and every white vertex using DFS-
‰ Explore “deeper”
deeper in the graph whenever possible
Vi i
Visit
„ Each call to DFS-Visit(u) roots a new tree of
‰ Edges are explored
Ed l d outt off th
the mostt recently
tl th d
the depth-first
th fi t forest
f t u
t att vertex
discovered vertex v that still has unexplored „ A vertex is white if it is undiscovered
edges
„ A vertex is green if it has been discovered but
not all of its edges have been discovered
‰ When allll off v’s
Wh ’ edges
d h
have b
been explored,
l d „ A vertex is black after all of its adjacent vertices
backtrack to the vertex from which v was have been discovered (the adj. list was
discovered examined
i d completely)
l t l )
41 42

DFS Example DFS Example


source source Discovery Finishing
Fi i hi
time
vertex vertex time
d f
1 | | |

| |

| | |

43 44
DFS Example DFS Example
source source
vertex vertex
d f d f
1 | | | 1 | | |

2 | | 2 | |

| | | 3 | | |

45 46

DFS Example DFS Example


source source
vertex vertex
d f d f
1 | | | 1 | | |

2 | | 2 | |

3 | 4 | | 3 | 4 5 | |

47 48
DFS Example DFS Example
source source
vertex vertex
d f d f
1 | | | 1 | | |

2 | | 2 | 7 |

3 | 4 5 | 6 | 3 | 4 5 | 6 |

49 50

DFS Example DFS Example


source source
vertex vertex
d f d f
1 | 8 | | 1 | 8 | |

2 | 7 | 2 | 7 9 |

3 | 4 5 | 6 | 3 | 4 5 | 6 |

51 52
DFS Example DFS Example
source source
vertex vertex
d f d f
1 | 8 | | 1 | 8 |11 |

2 | 7 9 |10 2 | 7 9 |10

3 | 4 5 | 6 | 3 | 4 5 | 6 |

53 54

DFS Example DFS Example


source source
vertex vertex
d f d f
1 |12 8 |11 | 1 |12 8 |11 13|

2 | 7 9 |10 2 | 7 9 |10

3 | 4 5 | 6 | 3 | 4 5 | 6 |

55 56
DFS Example DFS Example
source source
vertex vertex
d f d f
1 |12 8 |11 13| 1 |12 8 |11 13|

2 | 7 9 |10 2 | 7 9 |10

3 | 4 5 | 6 14| 3 | 4 5 | 6 14|15

57 58

DFS Example DFS Example


source
u v w u v w u v w
vertex
d f 1/ 1/ 2/ 1/ 2/

1 |12 8 |11 13|16 3/


x y z x y z x y z

2 | 7 9 |10 u v w u v w u v w
1/ 2/ 1/ 2/ 1/ 2/
B B
4/ 3/ 4/ 3/ 4/5 3/
3 | 4 5 | 6 14|15 x y z x y z x y z

59 60
DFS Example DFS Example
u v w u v w u v w u v w u v w u v w
1/ 2/ 1/ 2/7 1/ 2/7 1/8 2/7 9/ 1/8 2/7 9/ 1/8 2/7 9/
B C B C B C
B B F B F F F
4/5 3/6 4/5 3/6 4/5 3/6 4/5 3/6 10/ 4/5 3/6 10/ B 4/5 3/6 10/11 B
x y z x y z x y z x y z x y z x y z

u v w
u v w u v w u v w
1/8 2/7 9/12
1/8 2/7 1/8 2/7 9/ 1/8 2/7 9/ B C
B B B C F
F F F
4/5 3/6 10/11 B
4/5 3/6 4/5 3/6 4/5 3/6
x y z
x y z x y z x y z

61 62

You might also like