CmSc 250 Intro to Algorithms
Graph Algorithms : Summary
- Basic concepts
Definition:
A graph is a collection
(nonempty set) of vertices and edges
A path
from vertex x to vertex
y : a list of vertices in which successive vertices are connected
by edges
Connected graph:
There is a path between each two vertices
Simple path: No vertex is repeated
Cycle: Simple path except that the
first vertex is equal to the last
Loop: An edge that connects the vertex with itself
Tree: A graph with no cycles
Spanning tree of a graph: a
subgraph that contains all the vertices, and no cycles
Complete graphs:
Graphs with all edges present – each vertex is connected to all other vertices.
Weighted graphs –
weights
are assigned to each edge (e.g. road map with distances)
Directed graphs:
The edges are oriented, they have a beginning and an end
A tree with N vertices has N-1 edges.
A graph with less than N-1 edges is not connected.
Topological sort: an ordering of the vertices in a directed acyclic graph, such that:
If there is a path from u to v, then v appears after u in the ordering.
Types of graphs: directed, acyclic
Degree of a node U: the number of edges (U,V) - outgoing edges
Indegree of a node U: the number of edges (V,U) - incoming edges
Algorithm
Complexity
The number of operations is
O(|E| + |V|), |V| - number of vertices, |E| - number of edges.
How many operations are needed to compute the
indegrees?
Depends on the representation:
Algorithm for computing the distance for a vertex s to all other vertices:
Complexity:
Similar to the single source shortest path algorithm for unweighted graphs. Differences:
Algorithm:
s – starting node
DT – Distance Table,
PQ – priority queue, the priority of a node is equal
to the distance from s to that node
Initialize DT(s,0) = 0, DT(s,1) = 0, all remaining DT(j,k) = -1
If distance to w not computed (DT(w,0) = -1)
else
if old distance > new distance, i.e. DT(w,0) > new_distance
Complexity O(ElogV + VlogV) = O((E + V)log(V))
Similar to finding the shortest path in unweighted graphs
Data structures needed:
Algorithm
Complexity: O(E + V) - we process all edges and all nodes
The algorithm is similar to finding the shortest paths in weighted graphs.
The difference is that we record in the table the length of the current edge, not the length of the path .
Data structures needed:
Algorithm:
At the end of the algorithm, the tree would be represented in the table with its edges
{(Ti,3 , vi ) | i = 1, 2, ...,V}
Complexity: O(Elog( V ))
All edges have to be examined, and in the worst case each edge might
cause updating of the priority queue.
If adjacency matrix is used, the complexity would increase to
O(V2)
Kruskal's algorithm works with tree forests and the set of edges.
Algorithm:
Complexity of Kruskal's algorithm
A detailed analysis will show O(V) + O(Elog(E)) + O(Elog(V)).
Disregarding the lower term
O(V) we get O(E (log(V) + log(E)).
At the worst case E = O(V2). Hence
log(E) = O(log(V2)) = O(2log(V)) = O(log(V).
Thus we get complexity O(Elog(V)).
On
the other hand, V = O(E), hence we can reduce the
complexity expression to O(Elog(E)).
For sparse trees Kruskal's algorithm is better - since it is guided by the edges.
For dense trees Prim's algorithm is better - the process is limited by the number of the processed vertices (first column in the table to be F in order to process, otherwise we skip the vertex)
Activity:
a task, has duration and prerequisites
Event: a point in time,
characterized by the completion of one or several activities
A model of a project:
Directed simple acyclic graph
Simple graph means: each
pair of nodes connected by at most one edge
Nodes: events (Source: starting event, Sink: terminating event
Edges:
activities
Purpose of the model:
We use topological sorting to determine the above characteristics of the project.
The earliest occurrence time
of an event
EOT(j)= max(earliest completion times of
all activities preceding the event)
EOT(source) = 0
The earliest completion time
of an activity
ECT(j,k) is
equal to EOT(j) + duration(j,k)
Algorithm to compute EOT and ECT
ECT(i,j) = EOT(i)+ duration (i,j)
EOT(j) = max(EOT(j) , ECT(i,j))
Algorithm to find the slack of activities: latest occurrence time LOT(j)
LOT(sink) = EOT(sink)
For each non-sink event
i in the reverse topological
order:
Critical activities: slack(j) = 0
Critical path in the project: all edges are activities with slack = 0
Complexity: O(V + E) with adjacency lists, O(V2) with adjacency matrix
BFS algorithm
Read vertex vfrom the queue
For all neighbors w:
We can use a distance table to mark vertices as processed by storing the distance to the source vertex.
Complexity
In step 1 we read a node from the queue. Each node is stored only once – if it is not processed. Thus the reading step will be performed O(V) times.
In step 2 we examine all neighbors, i.e. we examine all edges of the currently read node. Since the graph is not oriented, we will examine 2*E edges, where E is the number of the edges in the graph.
Hence the complexity of BFS is O(V+ 2*E)
If we use adjacency matrix instead of adjacency lists, the complexity will be O(V2)
Attention: a typical error in determining the complexity is to multiply the number of the nodes by the number of the edges. Why we should not multiply the number of the edges by the number of the nodes?
For a selected vertex A let B1, B2, .. Bm be adjacent
vertices.
For all adjacent vertices, if Bkis not visited, visit
Bk and all vertices reachable from
Bk, before proceeding with Bk+1.
General algorithm:
Procedure dfs(s)
Procedure scan(s)
mark and visit s
for each neighbor w of s
Implementation
DepthFirst(Vertex v)
Visit(v)
The non-recursive implementation uses a stack
Initialization:
while the stack is not empty:
visit(v)
Connected graph: An undirected graph is said to be connected if there is a path between any two nodes
Biconnected graph: There are no vertices whose removal will disconnect the graph.
Articulation point (cut-vertex): A node whose removal disconnects the graph
Bridge: An edge whose removal disconnects the graph
Strongly connected graph: A directed graph where for any pair of nodes there is a path from each one to the other
Unilaterally connected graph: A directed graph where for any pair of nodes at least one of the nodes is reachable from the other
Weakly connected graph: A directed graph where the underlying undirected graph is connected
Each strongly connected graph is also unilaterally connected. Each unilaterally connected graph is also weakly connected.