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

Graphs, DFS, BFS, SP, ST Algorithms

The document discusses graph algorithms including definitions of graph terminology, representations of graphs using adjacency matrices and lists, and algorithms for solving problems on graphs like the shortest path problem (using Dijkstra's algorithm), minimum spanning trees (using Kruskal's algorithm), and graph traversal (using depth-first and breadth-first search). Key terms defined include vertices, edges, paths, cycles, connected/strongly connected graphs, and representations of graphs. Algorithms discussed include their time complexities and examples of their implementations.

Uploaded by

najiullah khan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Graphs, DFS, BFS, SP, ST Algorithms

The document discusses graph algorithms including definitions of graph terminology, representations of graphs using adjacency matrices and lists, and algorithms for solving problems on graphs like the shortest path problem (using Dijkstra's algorithm), minimum spanning trees (using Kruskal's algorithm), and graph traversal (using depth-first and breadth-first search). Key terms defined include vertices, edges, paths, cycles, connected/strongly connected graphs, and representations of graphs. Algorithms discussed include their time complexities and examples of their implementations.

Uploaded by

najiullah khan
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 31

Data Structures and

Algorithm Analysis
Graph Algorithms

Instructor: Javid Ali


Definitions
 A graph G=(V, E) consists of a set of vertices, V,
and a set of edges, E.

 Each edge is a pair (v, w), where v, wV. Edges


are sometimes referred to as arcs. If the pair is
ordered, then the graph is directed.

 Vertex w is adjacent to v if and only if (v, w)E.


In an undirected graph with edge {v, w}, w is
adjacent to v and v is adjacent to w. Some times
an edge has a third component, known as either a
weight or a cost.
Definitions
 A path in a graph is a sequence of vertices w1, w2,
w3, …, wN such that (wi, wi+1)E for 1i<N. The
length of such a path is the number of edges on the
path, which is equal to N-1.
 We allow a path from a vertex to itself; if this path
contains no edges, then the path length is 0.
 If the graph contains an edge (v, v) from a vertex to
itself, then the path v, v is sometime referred to as a
loop. The graphs we will consider will generally be
loopless.
 A simple path is a path such that all vertices are
distinct, except that the first and the last could be the
same.
Definitions
 A cycle in a directed graph is a path of length at
least 1 such that w1=wn; this cycle is simple if the
path is simple.

 For undirected graphs, we require that the edges be


distinct; that is, the path u, v, u in an undirected
graph should not be considered a cycle, because ( u,
v) and (v, u) are the same edge. In a directed
graph, these are different edges, so it makes sense
to call this a cycle.

 A directed graph is acyclic if it has no cycles.


Definitions
 An undirected graph is connected if there is a path
from every vertex to every other vertex. A directed
graph with this property is called strongly
connected. If a directed graph is not strongly
connected, but the underlying graph (without
direction to the arcs) is connected, then the graph is
said to be weakly connected.

 A complete graph is a graph in which there is an


edge between every pair of vertices.
Definitions
 In an undirected graph, the degree of a vertex is the
number of edges connected to this vertex.

 In an directed graph, the outdegree of a vertex is


the number of edges that start from this vertex, and
the indegree is the number of edges that end at this
vertex.

 Examples of graphs: airport system, traffic flow,


friendship.
Representation of Graphs
 Suppose, for now, that we can number the vertices,
starting at 1; that is, V={1, 2, …, n}
 One simple way to represent a graph is to use a two-
dimensional array this is known as a adjacency
matrix representation.
 For each edge (u, v), we set A[u][v]=1; otherwise
the entry in the array is 0.
 If the edge has a weight associated with it, then we
can set A[u][v] equal to the weight and use either a
very large or a very small weight as a sentinel to
indicate nonexistent edges.
Representation of Graphs
 If the number of edges in a graph is very small, a
better solution is an adjacency list representation.

 For each vertex, we keep a list of all adjacent


vertices: The leftmost structure is merely an array of
header cells. If the edges have weights, then this
additional information is also stored in the cells.
The Shortest Path Problem
 Let G=(V, E) be a directed graph in which each
edge has a nonnegative length, and a distinguished
vertex s called the source. The single-source
shortest path problem, or simply the shortest path
problem, is to determine the distance from s to
every other vertex in V, where the distance from
vertex s to vertex x is defined as the length of a
shortest path from s to x.

 For simplicity, we will assume that V={1, 2, …, n}


and s=1.

 This problem can be solved using a greedy


technique known as Dijkstra’s algorithm.
The Shortest Path Problem
 The set of vertices is partitioned into two sets X
and Y so that X is the set of vertices whose
distance from the source has already been
determined, while Y contains the rest vertices.
Thus, initially X={1} and Y={2, 3, …, n}.

 Associated with each vertex y in Y is a label [y],


which is the length of a shortest path that passes
only through vertices in X. Thus, initially

length 1, i  if (1, i )  E


[1]  0, [i ]   , 2in
 if (1, i)  E
The Shortest Path Problem
 At each step, we select a vertex yY with minimum  and
move it to X, and  of each vertex wY that is adjacent to
y is updated indicating that a shorter path to w via y has
been discovered.

w  Y and  y , w   E , [ w]  min [ w], [ y ]  length  y, w 

 The above process is repeated until Y is empty.

 Finally,  of each vertex in X is the distance from the


source vertex to this one.
The Shortest Path Problem
 Example:

3
2 4
1 15

9 4 6
1 13

4
12 5
3 5
The Shortest Path Problem
 Input: A weighted directed graph G=(V, E), where V={1, 2, …, n};
 Output: The distance from vertex 1 to every other vertex in G;
 1. X={1}; YV-{1}; [1]0;
 2. for y2 to n
 3. if y is adjacent to 1 then [y]length[1, y];
 4. else [y];
 5. end if;
 6. end for;
 7. for j1 to n-1
 8. Let yY be such that [y] is minimum;
 9. XX{y}; //add vertex y to X
 10. YY-{y}; //delete vertex y from Y
 11. for each edge (y, w)
 12. if wY and [y]+length[y, w]<[w] then
 13. [w][y]+length[y, w];
 14. end for;
 15. end for;
The Shortest Path Problem
 What’s the performance of the
DIJKSTRA algorithm?
 Time Complexity?
The Shortest Path Problem
 Given a directed graph G with nonnegative weights
on its edges and a source vertex s, Algorithm
DIJKSTRA finds the length of the distance from s to
every other vertex in (n2) time.
The Shortest Path Problem
 Write codes to implement the Dijkstra algorithm.
Minimum Cost Spanning
Trees (Kruskal’s Algorithm)
 Let G=(V, E) be a connected undirected graph with
weights on its edges.

 A spanning tree (V, T) of G is a subgraph of G that


is a tree.

 If G is weighted and the sum of the weights of the


edges in T is minimum, then (V, T) is called a
minimum cost spanning tree or simply a minimum
spanning tree.
Minimum Cost Spanning
Trees (Kruskal’s Algorithm)
 Kruskal’s algorithm works by maintaining a forest
consisting of several spanning trees that are
gradually merged until finally the forest consists of
exactly one tree.

 The algorithm starts by sorting the edges in


nondecreasing order by weight.
Minimum Cost Spanning
Trees (Kruskal’s Algorithm)
 Next, starting from the forest (V, T) consisting of
the vertices of the graph and none of its edges, the
following step is repeated until (V, T) is
transformed into a tree: Let (V, T) be the forest
constructed so far, and let eE-T be the current
edge being considered. If adding e to T does not
create a cycle, then include e in T; otherwise
discard e.

 This process will terminate after adding exactly n-1


edges.
Minimum Cost Spanning
Trees (Kruskal’s Algorithm)
 Example:
11
2 4
1 3

6 9 6
1 7

4
2 13
3 5
Minimum Cost Spanning
Trees (Kruskal’s Algorithm)
 Write codes to implement the Kruskal algorithm.
Graph Traversal
 In some cases, what is important is that the vertices
are visited in a systematic order, regardless of the
input graph. Usually, there are two methods of graph
traversal:

 Depth-first search
 Breadth-first search
Depth-First Search
 Let G=(V, E) be a directed or undirected graph.

 First, all vertices are marked unvisited.

 Next, a starting vertex is selected, say vV, and marked


visited. Let w be any vertex that is adjacent to v. We
mark w as visited and advance to another vertex, say x,
that is adjacent to w and is marked unvisited. Again, we
mark x as visited and advance to another vertex that is
adjacent to x and is marked unvisited.
Depth-First Search
 This process of selecting an unvisited vertex adjacent to the
current vertex continues as deep as possible until we find a
vertex y whose adjacent vertices have all been marked visited.
 At this point, we back up to the most recently visited vertex,
say z, and visit an unvisited vertex that is adjacent to z, if any.
 Continuing this way, we finally return back to the starting
vertex v.
 The algorithm for such a traversal can be written using
recursion.
Depth-First Search
 Example:

d a i

c b g h

e f j
Depth-First Search
 When the search is complete, if all vertices are reachable
from the start vertex, a spanning tree called the depth-
first search spanning tree is constructed whose edges are
those inspected in the forward direction, i.e., when
exploring unvisited vertices.
 As a result of the traversal, the edges of an undirected
graph G are classified into the following two types:
 Tree edges: edges in the depth-first search tree.
 Back edges: all other edges.
Depth-First Search
 Input: An undirected graph G=(V, E);
 Output: Preordering of the vertices in the corresponding depth-first
search tree.
 1. predfn0;
 2. for each vertex vV
 3. Mark v unvisited;
 4. end for;
 5. for each vertex vV
 6. if v is marked unvisited then dfs(v);
 7. end for;

 dfs(v)
 1. Mark v visited;
 2. predfnpredfn+1;
 3. for each edge (v, w)E
 4. if w is marked unvisited then dfs(w);
 5. end for;
Depth-First Search
 Write codes to implement the Depth-First Search
on graph.
Breadth-First Search
 When we visit a vertex v, we next visit all vertices
adjacent to v.

 This method of traversal can be implemented by a


queue to store unexamined vertices.
Breadth-First Search
 Example:

d a i

c b g h

e f j
Homework
 9.1
 9.3
 9.5a
 9.9
 9.15a (only Kruskal algorithm)
 9.18
 9.20
 9.21

You might also like