0% found this document useful (0 votes)
23 views

Graph

A graph consists of a set of vertices and edges. There are two main types: directed graphs where edges have orientations from one vertex to another, and undirected graphs where edges do not have orientations. Graphs can be represented using adjacency matrices and adjacency lists. Common graph algorithms include depth-first search (DFS) and breadth-first search (BFS) for traversing graphs, as well as finding minimum spanning trees and shortest paths.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Graph

A graph consists of a set of vertices and edges. There are two main types: directed graphs where edges have orientations from one vertex to another, and undirected graphs where edges do not have orientations. Graphs can be represented using adjacency matrices and adjacency lists. Common graph algorithms include depth-first search (DFS) and breadth-first search (BFS) for traversing graphs, as well as finding minimum spanning trees and shortest paths.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 116

+

Graph consists of a set of


vertices and a set of
edges.

G = ( V, E )
types of
directed
Edge (u,v) is incident from u.
Edge (u,v) is incident to v.
Vertex v is adjacent to vertex u.
undirected
Edge (u,v) or (v,u) is incident on u and v.
Vertex v is adjacent to vertex u.
Vertex u is adjacent to vertex v.
1

5
weighted 2
3 2 6 1

8
2
8

7 5
weighted
7
9
8

11
Path represents a
path in a sequence of edges
between the two
vertices.
path in a
path in a
in-degree, +(v)
# of edges incident to v

out-degree, -(v)
# of edges incident from v
degree of vertex v, (v)
# of edges incident on v
= -(v) + +(v)
degree of vertex v, (v)
# of edges incident on v.

(also applicable for undirected graphs)


+(4) : 3
+(7) : 2
-(7) : 1
-(5) : 2
(5) : 3
(4) : 6
Representations
adjacency 2D
adjacency list of
A B C D E
A 0 1 0 1 0
B 1 0 1 0 1
C 0 1 0 1 1
D 1 0 1 0 0
E 0 1 1 0 0
A B C D E
A ∞ 1 ∞ 1 ∞
B 1 ∞ 1 ∞ 1
C ∞ 1 ∞ 1 1
D 1 ∞ 1 ∞ ∞
E ∞ 1 1 ∞ ∞
A B C D E
A 0 1 0 1 0
B 0 0 0 0 1
C 0 1 0 0 1
D 0 0 1 0 0
E 0 0 0 0 0
A B C D E 4
A 0 2 0 4 0
2 5
B 0 0 0 0 7
C 0 3 0 0 1 3
7 1
D 0 0 5 0 0
E 0 0 0 0 0
An ordering of vertices in a
topological directed acyclic graph such
that if there is a path from vi
to vj, then vj appears after vi
in the ordering.
A, B, C, D, E
A, C, D, B, E
directed
A directed graph with no
acyclic graph
directed cycles.
directed
acyclic graph
directed
acyclic graph
Algorithm:

• Find any vertex with no incoming


edges (in-degree is 0).
• Print this vertex and remove it along
with its edges from the graph.
• Repeat steps above.
1
1
12
12
125
125
1254
1254
12547
12547
125473
125473
1254736
1254736
graph
search
search
ifp.uni-stuttgart.de
Generalization ofpreorder
traversal.
search
Starting at some vertex v,
process v and recursively
traverse all vertices
adjacent to v.
search
void DFS( vertex v, graph G ){
print v;
visited[v] = TRUE;
for each w adjacent to v:
if( !visited[w] )
DFS(w);
}
void DFS( vertex v, graph G ){
stack S;
push(v,S);
while stack S is not empty{
v = pop(S);
if ( !visited[v] ){ print v;
visited[v] = TRUE;
for each w adjacent to v:
if( !visited[w] )
push(w, S);
}
}
}
Result:
A
Result: A
B
A
Result: AB
E
B
A
Result: A B E
G
E
B
A
Result: A B E G
E
B
A
Result: A B E G
B
A
Result: A B E G
F
B
A
Result: A B E G F
C
F
B
A
Result: A B E G F C
H
C
F
B
A
Result: A B E G F C H
C
F
B
A
Result: A B E G F C H
F
B
A
Result: A B E G F C H
D
F
B
A
Result: A B E G F C H D
F
B
A
Result: A B E G F C H D
B
A
Result: A B E G F C H D
A
Result: A B E G F C H D
Result: A B E G F C H D
Detecting cycle in a graph.
Topological sorting.
applications Path finding.
Spanning trees.
A graph has a cycle if
detecting and only if we can find a
cycle in a back edge during DFS.
graph
Result/Visited:
A
Result/Visited: A
B
A
Result/Visited: AB
E
B
A
Result/Visited: A B E
G
E
B
A
Result/Visited: A B E G
Generalization of
level-order traversal
of trees.
search
void BFS( vertex v, graph G ){
queue Q;
enqueue(v,Q);
while queue Q is not empty{
v = dequeue(Q);
if ( !visited[v] ){
print v;
visited[v] = TRUE;
for each w adjacent to v:
if( !visited[w] )
enqueue(w, Q);
}
}
}
Result: A
A

Result: A
A B

Result: A B
A B D

Result: A B D
A B D G

Result: A B D G
B D G

Result: A B D G
B D G E

Result: A B D G E
B D G E F

Result: A B D G E F
D G E F

Result: A B D G E F
G E F

Result: A B D G E F
E F

Result: A B D G E F
F

Result: A B D G E F
F C

Result: A B D G E F C
C

Result: A B D G E F C
C H

Result: A B D G E F C H
H

Result: A B D G E F C H
Result: A B D G E F C H
Finding nodes within one.
connected components.
applications Testing for bipartiteness.
Finding shortest paths.
minimum
minimum A tree formed from graph
edges that connects all the
vertices of the graph at
lowest cost.
cost = ?
cost = 5
cost = 11
cost = 18
cost = 25
cost = 30
cost = 39

You might also like