FADML 06 PPC Graphs and Traversals PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

INTRODUCTION TO GRAPHS

Partha P Chakrabarti
Indian Institute of Technology Kharagpur
Graphs
A Graph G = (V, E) consists of the
following:
• A set of Vertices or Nodes V
– Nodes may have one or more labels
• A set of Edges E where each edge
connects vertices of V
– An edge usually defines a connection or
relationship between vertices or nodes
– The edges can be undirected or directed
– Each edge can have one or more labels
– Usually there is at most one edge between
vertices, there could be multiple edges
between the same nodes.
– Normally an edge connects two vertices, but
in general we could have hyper-edges
Graphs
A Graph G = (V, E) consists of the
following:
• A set of Vertices or Nodes V
– Nodes may have one or more labels
• A set of Edges E where each edge
connects vertices of V
– An edge usually defines a connection or
relationship between vertices or nodes
– The edges can be undirected or directed
– Each edge can have one or more labels
– Usually there is at most one edge between
vertices, there could be multiple edges
between the same nodes.
– Normally an edge connects two vertices, but
in general we could have hyper-edges
Some Applications of Graphs
• Maps, Routes
• Layouts
• Circuits and Networks
• Relationships
• Constraints
• Dependencies
• Flow Charts
• State Machines
Some Applications of Graphs
• Maps, Routes
• Layouts
• Circuits and Networks
• Relationships
• Constraints
• Dependencies
• Flow Charts
• State Machines
Some Applications of Graphs
• Maps, Routes
• Layouts
• Circuits and Networks
• Relationships
• Constraints
• Dependencies
• Flow Charts
• State Machines
Some Applications of Graphs
• Maps, Routes
• Layouts
• Circuits and Networks
• Relationships
• Constraints
• Dependencies
• Flow Charts
• State Machines
Some Applications of Graphs
• Maps, Routes
• Layouts
• Circuits and Networks
• Relationships
• Constraints
• Dependencies
• Flow Charts
• State Machines
Some Applications of Graphs
• Maps, Routes
• Layouts
• Circuits and Networks
• Relationships
• Constraints
• Dependencies
• Flow Charts
• State Machines
Some Applications of Graphs
• Maps, Routes
• Layouts
• Circuits and Networks
• Relationships
• Constraints
• Dependencies
• Flow Charts
• State Machines
Some Applications of Graphs
• Maps, Routes
• Layouts
• Circuits and Networks
• Relationships
• Constraints
• Dependencies
• Flow Charts
• State Machines
Some Applications of Graphs
• Maps, Routes
• Layouts
• Circuits and Networks
• Relationships
• Constraints
• Dependencies
• Flow Charts
• State Machines
Graph Representation
Adjacency List

Adjacency Matrix
Graph Representation
Adjacency List

Adjacency Matrix
Some Algorithms on Graphs
• Paths
• Reachability
• Connected
Components
• Trees, Cycles,
ordering
• Costs & Distances
• Spanning Trees
• Shortest Paths
• Flows
Some Algorithms on Graphs
• Paths
• Reachability
• Trees, Cycles,
ordering
• Connected
Components
• Costs & Distances
• Spanning Trees
• Shortest Paths
• Flows
Some Algorithms on Graphs
• Paths
• Reachability
• Cycles, ordering
• Connected
Components
• Costs & Distances
• Spanning Trees
• Shortest Paths
• Flows
Some Algorithms on Graphs
• Paths
• Reachability
• Cycles, ordering
• Connected
Components
• Costs & Distances
• Spanning Trees
• Shortest Paths
• Flows
Some Algorithms on Graphs
• Paths
• Reachability
• Cycles, ordering
• Connected
Components
• Costs & Distances
• Spanning Trees
• Shortest Paths
• Flows
Thank you
TRAVERSAL OF UNDIRECTED GRAPHS

Partha P Chakrabarti
Indian Institute of Technology Kharagpur
Undirected Graph
An Undirected Graph G = (V, E) consists of
the following:
• A set of Vertices or Nodes V
• A set of Edges E where each edge
connects two vertices of V
Example: Figure 1
V = {0,1,2,3,4,5,6,7,8} Figure 1
E = {(0,1), (0,8), (0,3),(1,7), (2,3), (2,5), (2,7),
(3,4), (4,8), (5,6)}
Successor Function: succ(i) = {set of nodes
to which node i is connected}
Example: Succ(2) = {3,5,7}
Weighted Undirected Graphs: Such Graphs
may have weights on edges (Figure 2)
Figure 2
Problems on Undirected Graphs
Reachability, Path, Cycle / Tree Detection,
Connected Components, Bi-Connected
Components, Spanning Tree, Shortest
Path, Maximum Flow, Vertex Cover, Edge
Cover, Travelling Salesperson,

Figure 1

Figure 2
Basic Traversal Algorithm (Depth First Search) I
Global Data: G = (V,E)
visited [i] indicates if node i is visited. For
all nodes j visited [j] is initialized to 0
succ(i) = {set of nodes to which node i is
connected}
Dfs(node) {
visited[node] = 1;
for each j in succ(node) do {
if (visited [j] ==0) Dfs(j)
}
}
Basic Traversal Algorithm (Depth First Search) II
Global Data: G = (V,E)
visited [i] indicates if node i is visited. For
all nodes j visited [j] is initialized to 0
succ(i) = {set of nodes to which node i is
connected}
Dfs(node) {
visited[node] = 1;
for each j in succ(node) do {
if (visited [j] ==0) Dfs(j)
}
}
Basic Traversal Algorithm (Depth First Search) III
Global Data: G = (V,E)
visited [i] indicates if node i is visited. For
all nodes j visited [j] is initialized to 0
succ(i) = {set of nodes to which node i is
connected}
Dfs(node) {
visited[node] = 1;
for each j in succ(node) do {
if (visited [j] ==0) Dfs(j)
}
}
Cycle Detection
Global Data: G = (V,E)
visited [i] indicates if node i is visited. For all
nodes j visited [j] is initialized to 0
succ(i) = {set of nodes to which node i is
connected}
Dfs(node) {
visited[node] = 1;
for each j in succ(node) do {

if (visited [j] ==0) Dfs(j)

}
}
// Cycle Detection //
Path Finding
Global Data: G = (V,E)
visited [i] indicates if node i is visited. For all nodes
j visited [j] is initialized to 0

succ(i) = {set of nodes to which node i is


connected}
Dfs(node) {
visited[node] = 1;
for each j in succ(node) do {
if (visited [j] ==0) {

Dfs(j) }
}
}
// Tree Edge, Back Edge, Parent Links, Tracing
Paths //
Connected Components
Global Data: G = (V,E)
Visited[i], comp[i] all initialized to 0
count = 0;

Algorithm components() {
for each node k do {
if visited [k] == 0 { count = count + 1;
DfComp_S(k) }

DfComp(node) {
visited[node] = 1; comp[node] = count;
for each j in succ(node) do {
if (visited [j] ==0) DfComp(j)
}
}
Depth-First Numbering & Time Stamping
Global Data: G = (V,E)
Visited[i], comp[i] all initialized to 0
count = 0;

Algorithm components() {
for each node k do {
if visited [k] == 0 { count = count + 1;
DfComp_S(k) }

DfComp(node) {
visited[node] = 1; comp[node] = count;
for each j in succ(node) do {
if (visited [j] ==0) {

DfComp(j)

}
}
}
Breadth-First Search
Global Data: G = (V,E)
Visited[i] all initialized to 0
Queue Q initially {}
BFS(k) {
visited [k] = 1; Q = {k};
While Q != {} {
j = DeQueue (Q);
if visited[j] == 0 {
visited [j] = 1;
For each k in succ (j) EnQueue(Q,k);
}
}
/Parent links, Shortest Length Path Finding in
unweighted graphs/
Pathfinding in Weighted Undirected Graphs I
Global Data: G = (V,E)
Visited[i] all initialized to 0,
Cost[j] all initialized to INFINITY
Ordered Queue Q initially {}
BFSW(k) {
visited [k] = 1; cost [k] = 0; Q = {k};
While Q != {} {
j = DeQueue (Q);
if visited[j] == 0 {
visited [j] = 1;
For each k in succ (j) {
if cost[k] > cost[j] + c[j,k]
cost[k] = cost[j] + c[j,k];
EnQueue(Q,k);}
}
}
Pathfinding in Weighted Undirected Graphs II
Global Data: G = (V,E)
Visited[i] all initialized to 0,
Cost[j] all initialized to INFINITY
Ordered Queue Q initially {}
BFSW(k) {
visited [k] = 1; cost [k] = 0; Q = {k};
While Q != {} {
j = DeQueue (Q);
if visited[j] == 0 {
visited [j] = 1;
For each k in succ (j) {
if cost[k] > cost[j] + c[j,k]
cost[k] = cost[j] + c[j,k];
EnQueue(Q,k);}
}
}
Thank you
TRAVERSAL OF DIRECTED GRAPHS

Partha P Chakrabarti
Indian Institute of Technology Kharagpur
Directed Graphs
An Undirected Graph G = (V, E) consists of
the following:
• A set of Vertices or Nodes V
• A set of DIRECTED Edges E where each
edge connects two vertices of V. The
edge is an ORDERED pair of vertices
Successor Function: succ(i) = {set of nodes Figure 1
to which node i is connected}
Directed Acyclic Graphs (DAGs): Such
Graphs have no cycles (Figure 2)
Weighted Undirected Graphs: Such Graphs
may have weights on edges (Figure 3). We
can also have Weighted DAGs
Figure 2 Figure 3
Basic Traversal Algorithm (Depth First Search)
Global Data: G = (V,E)
visited [i] indicates if node i is visited. / initially 0 /
Parent[i] = parent of a node in the Search / initially
NULL /
succ(i) = {set of nodes to which node i is
connected}
Dfs(node) {
visited[node] = 1;
for each j in succ(node) do {
if (visited [j] ==0) { Parent[j] = node;
Dfs(j) }
}
}
Traversing the Complete Graph by DFS
Global Data: G = (V,E)
visited [i] indicates if node i is visited. / initially 0 /
Parent[i] = parent of a node in the Search / initially
NULL /
succ(i) = {set of nodes to which node i is
connected}
Dfs(node) {
visited[node] = 1;
for each j in succ(node) do {
if (visited [j] ==0) { Parent[j] = node;
Dfs(j) }
}
}
Entry-Exit Numbering
Global Data: G = (V,E)
visited [i] indicates if node i is visited. / initially 0 /
Parent[i] = parent of a node in the Search / initially
NULL /
Entry[i] = node entry sequence / initially 0 /
Exit[i] = node exit sequence / initially 0 /
succ(i) = {set of nodes to which node i is connected}
numb = 0;
Dfs(node) {
visited[node] = 1; numb = numb+1;
Entry[node] = numb;
for each j in succ(node) do
if (visited [j] ==0) { Parent[j] = node;
Dfs(j) }
numb = numb + 1;
Exit[node] = numb;
}
Tree Edge, Back Edge, Forward Edge, Cross Edge
Global Data: G = (V,E)
visited [i] indicates if node i is visited. / initially 0 /
Parent[i] = parent of a node in the Search / initially NULL
/
Entry[i] = node entry sequence / initially 0 /
Exit[i] = node exit sequence / initially 0 /
succ(i) = {set of nodes to which node i is connected}
numb = 0;
Dfs(node) {
visited[node] = 1; numb = numb+1; Edge (u,v) is
Entry[node] = numb; Tree Edge or Forward Edge: if & only if
for each j in succ(node) do Entry[u] < Entry[v] < Exit[v] < Exit[u]
if (visited [j] ==0) { Parent[j] = node; Back Edge: if & only if
Dfs(j) } Entry[v] < Entry [u] < Exit [u] < Exit [v]
numb = numb + 1; Cross Edge: if & only if
Exit[node] = numb; Entry [v] < Exit [v] < Entry [u] < Exit [u]
}
Reachability, Paths, Cycles, Components
Global Data: G = (V,E)
visited [i] indicates if node i is visited. / initially 0 /
Parent[i] = parent of a node in the Search / initially
NULL /
Entry[i] = node entry sequence / initially 0 /
Exit[i] = node exit sequence / initially 0 /
succ(i) = {set of nodes to which node i is connected}
numb = 0;
Dfs(node) {
visited[node] = 1; numb = numb+1;
Edge (u,v) is
Entry[node] = numb; Tree Edge or Forward Edge: if & only if
for each j in succ(node) do Entry[u] < Entry[v] < Exit[v] < Exit[u]
if (visited [j] ==0) { Parent[j] = node; Back Edge: if & only if
Dfs(j) } Entry[v] < Entry [u] < Exit [u] < Exit [v]
numb = numb + 1; Cross Edge: if & only if
Exit[node] = numb; Entry [v] < Exit [v] < Entry [u] < Exit [u]
}
Directed Acyclic Graphs
Global Data: G = (V,E)
visited [i] indicates if node i is visited. / initially 0 /
Parent[i] = parent of a node in the Search / initially
NULL /
Entry[i] = node entry sequence / initially 0 /
Exit[i] = node exit sequence / initially 0 /
succ(i) = {set of nodes to which node i is connected}
numb = 0;
Dfs(node) {
visited[node] = 1; numb = numb+1;
Entry[node] = numb;
for each j in succ(node) do
if (visited [j] ==0) { Parent[j] = node;
Dfs(j) }
numb = numb + 1;
Exit[node] = numb;
}
Topological Ordering, Level Values
Global Data: G = (V,E)
visited [i] indicates if node i is visited. / initially 0 /
Parent[i] = parent of a node in the Search / initially
NULL /
Entry[i] = node entry sequence / initially 0 /
Exit[i] = node exit sequence / initially 0 /
succ(i) = {set of nodes to which node i is connected}
numb = 0; numb1 = 0;
Dfs(node) {
visited[node] = 1; numb = numb+1;
Entry[node] = numb;
for each j in succ(node) do
if (visited [j] ==0) { Parent[j] = node;
Dfs(j) }
numb1 = numb1 + 1;
Exit[node] = numb1;
}
Shortest Cost Path in Weighted DAGs
Breadth-First Search
Global Data: G = (V,E)
Visited[i] all initialized to 0
Queue Q initially {}
BFS(k) {
visited [k] = 0; Q = {k};
While Q != {} {
j = DeQueue (Q);
if visited[j] == 0 {
visited [j] = 1;
For each k in succ (j) {
if (visited[k]==0) EnQueue(Q,k); }
}
}
/Parent links, Shortest Length Path Finding in
unweighted directed graphs/
Pathfinding in Weighted Directed Graphs
Global Data: G = (V,E)
Visited[i] all initialized to 0,
Cost[j] all initialized to INFINITY
Ordered Queue Q initially {}
BFSW(k) {
visited [k] = 0; cost [k] = 0; Q = {k};
While Q != {} {
j = DeQueue (Q);
if visited[j] == 0 {
visited [j] = 1;
For each k in succ (j) {
if cost[k] > cost[j] + c[j,k]
cost[k] = cost[j] + c[j,k];
EnQueue(Q,k);}
}
}
Thank you

You might also like