Graph Lec
Graph Lec
1
GRAPH – Definitions
• A graph G = (V, E) consists of
– a set of vertices, V, and
– a set of edges, E, where each edge is a pair (v,w) s.t. v,w V
• Vertices are sometimes called nodes, edges are sometimes called arcs.
• If the edge pair is ordered then the graph is called a directed graph
(also called digraphs) .
• We also call a normal graph (which is not a directed graph) an
undirected graph.
– When we say graph we mean that it is an undirected graph.
2
Graph – Definitions
• Two vertices of a graph are adjacent if they are joined by an edge.
• Vertex w is adjacent to v iff (v,w) E.
– In an undirected graph with edge (v, w) and hence (w,v) w is adjacent to v and v is
adjacent to w.
• A path between two vertices is a sequence of edges that begins at one
vertex and ends at another vertex.
– i.e. w1, w2, …, wN is a path if (wi, wi+1) E for 1 i . N-1
• A simple path passes through a vertex only once.
• A cycle is a path that begins and ends at the same vertex.
• A simple cycle is a cycle that does not pass through other vertices more
than once.
3
Graph – An Example
1 2
A graph G (undirected)
3 4 5
• Adjacent:
1 and 2 are adjacent -- 1 is adjacent to 2 and 2 is adjacent to 1
• Path:
1,2,5 ( a simple path), 1,3,4,1,2,5 (a path but not a simple path)
• Cycle:
1,3,4,1 (a simple cycle), 1,3,4,1,4,1 (cycle, but not simple cycle)
4
Graph -- Definitions
• A connected graph has a path between each pair of distinct vertices.
• A complete graph has an edge between each pair of distinct vertices.
– A complete graph is also a connected graph. But a connected graph may not be a complete
graph.
6
Directed Graph – An Example
1 2
3 4 5
• Adjacent:
2 is adjacent to 1, but 1 is NOT adjacent to 2
• Path:
1,2,5 ( a directed path),
• Cycle:
1,4,3,1 (a directed cycle),
7
Weighted Graph
• We can label the edges of a graph with numeric values, the graph is
called a weighted graph.
8
1 2 Weighted (Undirected) Graph
10 3 6
5 7
3 4 5
1 8 2
Weighted Directed Graph
10 3 6
3
5 4
7 5
8
Graph Implementations
• The two most common implementations of a graph are:
– Adjacency Matrix
• A two dimensional array
– Adjacency List
• For each vertex we keep a list of adjacent vertices
9
Adjacency Matrix
• An adjacency matrix for a graph with n vertices numbered 0,1,...,n-1 is
an n by n array matrix such that matrix[i][j] is 1 (true) if there is an
edge from vertex i to vertex j, and 0 (false) otherwise.
• When the graph is weighted, we can let matrix[i][j] be the weight that
labels the edge from vertex i to vertex j, instead of simply 1, and let
matrix[i][j] equal to instead of 0 when there is no edge from vertex i
to vertex j.
• Adjacency matrix for an undirected graph is symmetrical.
– i.e. matrix[i][j] is equal to matrix[j][i]
• Space requirement O(|V|2)
• Acceptable if the graph is dense.
10
Adjacency Matrix – Example1
A directed graph Its adjacency matrix
11
Adjacency Matrix – Example2
12
Adjacency List
• An adjacency list for a graph with n vertices numbered 0,1,...,n-1
consists of n linked lists. The ith linked list has a node for vertex j if and
only if the graph contains an edge from vertex i to vertex j.
• Adjacency list is a better solution if the graph is sparse.
• Space requirement is O(|E| + |V|), which is linear in the size of the
graph.
• In an undirected graph each edge (v,w) appears in two lists.
– Space requirement is doubled.
13
Adjacency List – Example1
A directed graph Its Adjacency List
14
Adjacency List – Example2
15
Adjacency Matrix vs Adjacency List
• Two common graph operations:
1. Determine whether there is an edge from vertex i to vertex j.
2. Find all vertices adjacent to a given vertex i.
16
Graph Traversals
• A graph-traversal algorithm starts from a vertex v, visits all of the
vertices that can be reachable from the vertex v.
• A graph-traversal algorithm visits all vertices if and only if the graph is
connected.
• A connected component is the subset of vertices visited during a
traversal algorithm that begins at a given vertex.
• A graph-traversal algorithm must mark each vertex during a visit and
must never visit a vertex more than once.
– Thus, if a graph contains a cycle, the graph-traversal algorithm can avoid infinite loop.
• We look at two graph-traversal algorithms:
– Depth-First Search
– Breadth-First Search
17
Depth-First Search
• For a given vertex v, the depth-first search algorithm proceeds along a
path from v as deeply into the graph as possible before backing up.
• That is, after visiting a vertex v, the depth-first search algorithm visits
(if possible) an unvisited adjacent vertex to vertex v.
• The depth-first traversal algorithm does not completely specify the
order in which it should visit the vertices adjacent to v.
– We may visit the vertices adjacent to v in sorted order.
18
Depth-First Search – Example
19
Depth-First Traversal Implementation
}
}
21
Example
• Demonstrates depth-first traversal using an explicit stack.
Order of
Traversal A B C F E G D H I Stack
Breadth-First Search
• After visiting a given vertex v, the breadth-first search algorithm visits
every vertex adjacent to v that it can before visiting any other vertex.
• The breadth-first traversal algorithm does not completely specify the
order in which it should visit the vertices adjacent to v.
– We may visit the vertices adjacent to v in sorted order.
23
Breadth-First Search– Example
24
Iterative Breadth-First Search Algorithm
bfs(in v:Vertex) {
// Traverses a graph beginning at vertex v
// by using breath-first strategy: Iterative Version
q.createQueue();
// add v to the queue and mark it
q.enqueue(v);
Mark v as visited;
while (!q.isEmpty()) {
q.dequeue(w);
for (each unvisited vertex u adjacent to w) {
Mark u as visited;
q.enqueue(u);
}
}
}
25
Trace of Iterative BFT – starting from vertex a
2
1
5
9
4
6
8
26
Exercise
d
a) Give the sequence of vertices when they
are traversed starting from the vertex a using
b the depth-first search algorithm.
e
a h
27
Some Graph Algorithms
• Shortest Path Algorithms
– Unweighted shortest paths
– Weighted shortest paths (Dijkstra’s Algorithm)
• Topological sorting
• Network Flow Problems
• Minimum Spanning Tree
• Depth-first search Applications
28
Unweighted Shortest-Path problem
• Find the shortest path (measured by number of
edges) from a designated vertex S to every
vertex.
1 2
3 4 5
6 7
29
Unweighted Shortest Path
Ds = 0, for all other vertices Dw = ; Dv = Distance from s to v
Create a Queue (Q) ; Enqueue(s, Q); // Insert starting vertex //
While (Q Not Empty) // for all vertices //
v = Dequeue(Q) // Delete vertex from Queue //
for each w adjacent to v
if (Dw = = ) {
Dw = Dv + 1; // increase no. of edges on path //
Pw = v ; // predecessor vertex for Path //
Enqueue(w, Q); //insert vertex for processing //
}
Figure 14.21A
Searching the graph in the unweighted shortest-path computation. The darkest-
shaded vertices have already been completely processed, the lightest-shaded
vertices have not yet been used as v, and the medium-shaded vertex is the current
vertex, v. The stages proceed left to right, top to bottom, as numbered (continued).
31
Figure 14.21B
Searching the graph in the unweighted shortest-path computation. The darkest-
shaded vertices have already been completely processed, the lightest-shaded
vertices have not yet been used as v, and the medium-shaded vertex is the current
vertex, v. The stages proceed left to right, top to bottom, as numbered.
32
Unweighted shortest path algorithm
void Graph::unweighted_shortest_paths(vertex s)
{
//An application of BFS: Finding the shortest path between
//two nodes u and v, with path length measured by # of edges
Queue<Vertex> q;
Vertex v,w;
for each Vertex v
v.dist = INFINITY;
s.dist = 0;
q.enqueue(s);
while (!q.isEmpty())
{
v= q.dequeue();
v.known = true; // not needed anymore
for each w adjacent to v
if (w.dist == INFINITY)
{
w.dist = v.dist + 1;
w.path = v;
q.enqueue(w);
33
}
} }