CE100 Algorithms and Programming II

CE100 Algorithms and Programming II

Week-10 (Graphs)

Spring Semester, 2021-2022

Download DOC-PDF, DOC-DOCX, SLIDE, PPTX

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphs

Outline

  • Introduction to Graphs
  • Graphs and Representation
  • BFS (Breath-First Search)
  • DFS (Depth-First Search)
    • in-order
    • post-order
    • pre-order
RTEU CE100 Week-10
CE100 Algorithms and Programming II
  • Topological Order
  • SCC (Strongly Connected Components)
  • MST
    • Prim
    • Kruskal
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs

  • The graph is a non-linear data structure.

  • It contains a set of points known as

    • nodes (or vertices) and

    • a set of links known as edges (or Arcs).

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs

  • Here edges are used to connect the vertices. A graph is defined as follows.
  • Generally, a graph GG is represented as G=(V,E)G=(V,E), where
    • VV is set of vertices and
    • EE is set of edges.

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs - Example

  • The following is a graph with 5 vertices (VV) and 6 edges (EE).
  • This graph G can be defined as

G=(V,E)\begin{aligned} G&=(V,E) \end{aligned}

V={A,B,C,D,E}\begin{align*} V&=\{A,B,C,D,E\}& \end{align*}

E={(A,B),(A,C),(A,D),(B,D),(C,D),(B,E),(E,D)}\begin{align*} E=\{&(A,B),(A,C),(A,D),\\ &(B,D),(C,D),(B,E),\\ &(E,D)\} \end{align*}

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Vertex

Individual data element of a graph is called as Vertex. Vertex is also known as node. In above example graph, A,B,C,D,EA, B, C, D, E are known as vertices.

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Edge

  • An edge is a connecting link between two vertices.
  • Edge is also known as Arc.
  • An edge is represented as

(startingVertex,endingVertex)(\text{startingVertex}, \text{endingVertex})

  • For example, in above graph the link between vertices AA and BB is represented as

(A,B)(A,B)

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Edge

  • In example graph, there are 77 edges

(A,B),(A,C),(A,D),(B,D),(B,E),(C,D),(D,E)(A,B),(A,C),(A,D),\\ (B,D),(B,E),(C,D),(D,E)

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Edge

  • Edges are three types.

    • Undirected Edge
    • Directed Edge
    • Weighted Edge
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Edge

Undirected Edge

  • An undirected egde is a bidirectional edge. If there is undirected edge between vertices AA and BB then edge (A,B)(A,B) is equal to edge (B,A)(B,A)

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Edge

Directed Edge

  • A directed egde is a unidirectional edge. If there is directed edge between vertices A and B then edge (A,B)(A,B) is not equal to edge (B,A)(B,A).

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Edge

Weighted Edge

  • A weighted egde is a edge with value (cost) on it.

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Undirected Graph

  • A graph with only undirected edges is said to be undirected graph.

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Directed Graph

  • A graph with only directed edges is said to be directed graph.

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Mixed Graph

  • A graph with both undirected and directed edges is said to be mixed graph.

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

End vertices or Endpoints

  • The two vertices joined by edge are called end vertices (or endpoints) of that edge.

  • In graph theory, a vertex with degree 1 is called an end vertex (plural end vertices)

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Origin

  • If a edge is directed, its first endpoint is said to be the origin of it.

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Destination

  • If a edge is directed, its first endpoint is said to be the origin of it and the other endpoint is said to be the destination of that edge.

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Adjacent

  • If there is an edge between vertices AA and BB then both AA and BB are said to be adjacent. In other words, vertices A and B are said to be adjacent if there is an edge between them.

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Incident

  • Edge/Arc is said to be incident on a Vertex/Node if the Vertex/Node is one of the endpoints of that Edge/Arc.

  • An incidence is a pair (B,e1)(B, e1) where BB is a vertex and e1e1 is an edge incident to BB

  • Two distinct incidences (B,e1)(B, e1) and (v,e2)(v,e2) are adjacent if and only if B=vB = v, e1=e2e1 = e2 or BB=e1BB' = e1 or e2e2.

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Outgoing Edge

  • A directed edge is said to be outgoing edge on its origin vertex.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Incoming Edge

  • A directed edge is said to be incoming edge on its destination vertex.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Degree

  • Total number of edges connected to a vertex is said to be degree of that vertex.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Indegree

  • Total number of incoming edges connected to a vertex is said to be indegree of that vertex.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Outdegree

  • Total number of outgoing edges connected to a vertex is said to be outdegree of that vertex.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Parallel edges or Multiple edges

  • If there are two undirected edges with same end vertices and two directed edges with same origin and destination, such edges are called parallel edges or multiple edges.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Self-loop

  • Edge (undirected or directed) is a self-loop if its two endpoints coincide with each other.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Simple Graph

  • A graph is said to be simple if there are no parallel and self-loop edges.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Complex Graph

  • A graph is said to be complex if there are parallel or self-loop edges.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Terminology

Path

  • A path is a sequence of alternate vertices and edges that starts at a vertex and ends at other vertex such that each edge is incident to its predecessor and successor vertex.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Representations

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Representations

  • Graph data structure is represented using following representations
    • Adjacency Matrix
    • Incidence Matrix
    • Adjacency List
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Representations

Adjacency Matrix

  • In this representation, the graph is represented using a matrix of size total number of vertices by a total number of vertices.
  • That means a graph with 4 vertices is represented using a matrix of size 4X4.
  • In this matrix, both rows and columns represent vertices.
    • This matrix is filled with either 1 or 0.
    • Here,
      • 1 represents that there is an edge from row vertex to column vertex and
      • 0 represents that there is no edge from row vertex to column vertex.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Representations

Adjacency Matrix

  • Undirected Graph

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Representations

Adjacency Matrix

  • Directed Graph

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Representations

Incidence Matrix

  • In this representation, the graph is represented using a matrix of size total number of vertices by a total number of edges.
  • That means graph with 4 vertices and 6 edges is represented using a matrix of size 4X6.
  • In this matrix, rows represent vertices and columns represents edges.
  • This matrix is filled with 0 or 1 or -1.
    • Here,
      • 0 represents that the row edge is not connected to column vertex,
      • 1 represents that the row edge is connected as the outgoing edge to column vertex and
      • -1 represents that the row edge is connected as the incoming edge to column vertex.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Representations

Incidence Matrix

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Representations

Adjacency List

  • In this representation, every vertex of a graph contains list of its adjacent vertices.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Representations

Adjacency List

  • Linked List Implementation

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Representations

Adjacency List

  • Reference Array Implementation

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Representations - Review

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Representations - Review

  • The standard two ways to represent a graph G=(V,E)G=(V,E)
    • As a collection of adjacency-lists
    • As an adjacency-matrix
  • Adjacency-list representation is usually preferred
  • Provides a compact way to represent sparse graphs
    • Those graphs for which E<<V2|E| << |V|^2
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Representations - Review

  • Adjacency-matrix representation may be preferred
    • for dense graphs for which E|E| is close to V2|V|^2
    • when we need to be able to tell quickly if there is an edge connecting two given vertices
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Adjacency-List Representation - Review

  • An array AdjAdj of V|V| lists, one for each vertex uVu \in V
  • For each uVu \in V the adjacency-list Adj[u]Adj[u] contains (pointers to) all vertices vv such that (u,v)E(u,v) \in E
  • That is, Adj[u]Adj[u] consists of all vertices adjacent to uu in GG
  • The vertices in each adjacency-list are stored in an arbitrary order
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Adjacency-List Representation - Review

  • If GG is a directed graph
    • The sum of the lengths of the adjacency lists =E=|E|
  • If GG is an undirected graph
    • The sum of the lengths of the adjacency lists =2E=2|E|
    • since an edge (u,v)(u,v) appears in both Adj[u]Adj[u] and Adj[v]Adj[v]
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Undirected Graphs Representations - Review

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Directed Graphs Representations - Review

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Adjacency List Representation (continued) - Review

  • Adjacency list representation has the desirable property
    • it requires O(max(V,E))=O(V+E)O(max(V,E)) = O(V+E) memory
    • for both undirected and directed graphs
  • Adjacency lists can be adopted to represent weighted graphs
    • each edge has an associated weight typically given by a weight function w:ERw: E \rightarrow R
  • The weight w(u,v)w(u, v) of an edge (u,v)E(u, v) \in E is simply stored with
    • vertex vv in Adj[u]Adj[u] or with
    • vertex uu in Adj[v]Adj[v] or both
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Adjacency List Representation (continued) - Review

  • A potential disadvantage of adjacency list representation
    • there is no quicker way to determine if a given edge (u,v)(u, v) is present in G than to search vv in Adj[u]Adj[u] or uu in Adj[v]Adj[v]
  • This disadvantage can be remedied by an adjacency matrix representation at the cost of using asymptotically more memory
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Adjacency Matrix Representation - Review

  • Assume that, the vertices of G=(V,E)G=(V, E) are numbered as 1,2,,V1,2,\dots,|V|
  • Adjacency matrix rep. consists of a V×V|V|\times|V| matrix A=(aij)A=(a_{ij}) \backepsilon

aij={1if (i,j)E0otherwisea_{ij}= \begin{cases} 1 & \text{if} \ (i,j) \in E \\ 0 & otherwise \end{cases}

  • Requires Θ(V2)\Theta(V^2) memory independent of the number of edges E|E|
  • We define the transpose of a matrix A=(aij)A=(a_{ij}) to be the matrix
    • AT=(aij)TA^T = (a_{ij})^T given by aijT=ajia_{ij}^T = a_{ji}
  • Since in an undirected graph, (u,v)(u,v) and (v,u)(v,u) represent the same edge A=ATA = A^T for an undirected graph
  • That is, adjacency matrix of an undirected graph is symmetric
  • Hence, in some applications, only upper triangular part is stored
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Adjacency Matrix Representation - Review

  • Adjacency matrix representation can also be used for
    • weighted graphs

aij={w(i,j)if (i,j)ENIL or 0 or otherwisea_{ij}= \begin{cases} w(i,j) & \text{if} \ (i,j) \in E \\ NIL \ or \ 0 \ or \ \infty & otherwise \end{cases}

  • Adjacency matrix may also be preferable for
    • reasonably small graphs
  • Moreover, if the graph is unweighted
    • rather than using one word of memory for each matrix entry adjacency matrix representation uses one bit per entry
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs - Review

G=(V,E)G=(V,E)

  • Adjency List Complexity O(degree of u)O(degree \ of \ u) (u,v)E(u,v) \in E
  • Sparse Matrix \rightarrow E<V2|E|<|V^2|
  • Dense Matrix \rightarrow E close to V2|E| \ close \ to \ |V^2|
  • Space Complexity Θ(V+E)\Theta(|V|+|E|)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs - Review

  • Many definitions for directed and undirected graphs are the same although certain terms have slightly different meanings
  • If (u,v)E(u,v) \in E in a directed graph G=(V,E)G=(V,E), we say that (u,v)(u,v) is incident from or leaves vertex uu and is incident to or enters vertex vv
  • If (u,v)E(u,v) \in E in an undirected graph G=(V,E)G=(V,E), we say that (u,v)(u,v) is incident on vertices uu and vv
  • If (u,v)(u,v) is an edge in a graph G=(V,E)G=(V,E), we say that vertex vv is adjacent to vertex uu
  • When the graph is undirected,the adjacency relation is symmetric
  • When the graph is directed
    • the adjacency relation is not necessarily symmetric
    • if vv is adjacent to uu, we sometimes write uvu \rightarrow v
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs - Review

  • The degree of a vertex in an undirected graph is the number of edges incident on it
  • In a directed graph,
    • out-degree of a vertex: number of edges leaving it
    • in-degree of a vertex: number of edges entering it
    • degree of a vertex: its in-degree + its out-degree
  • A path of length kk from a vertex uu to a vertex uu' in a graph G=(V,E)G=(V,E) is a sequence v0,v1,v2,,vk\langle v_0,v_1,v_2,\dots,v_k \rangle of vertices such
    • that v0=uv_0=u,vk=uv_k=u' and (vi1,vi)E(v_{i-1},v_i) \in E, for i=1,2,,ki=1,2,\dots,k
  • The length of a path is the number of edges in the path
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs - Review

  • If there is a path pp from uu to uu', we say that uu' is reachable from uu via p:upup: u \xrightarrow[]{p} u'
  • A path is simple if all vertices in the path are distinct
  • A subpath of path p=v0,v1,v2,,vkp= \langle v_0,v_1,v_2,\dots,v_k \rangle is a contiguous subsequence of its vertices
  • That is, for any 0ijk0 \leq i \leq j \leq k, the subsequence of vertices vi,vi+1,,vj\langle v_i, v_{i+1},\dots, v_j \rangle is a subpath of pp
  • In a directed graph, a path v0,v1,,vk\langle v_0,v_1,\dots, v_k\rangle forms a cycle if v0=vkv_0=v_k and the path contains at least one edge
  • The cycle is simple if, in addition, v0,v1,,vkv_0,v_1,\dots,v_k are distinct
  • A self-loop is a cycle of length 1
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs - Review

Two paths v0,v1,v2,,vk\langle v_0,v_1,v_2,\dots,v_k\rangle & v0,v1,v2,,vk\langle v_0',v_1',v_2',\dots,v_k'\rangle form the same cycle if there is an integer jj such that vi=v(i+j) mod kv_i' = v_{(i+j)\ \text{mod} \ k } for i=0,1,,k1i=0,1,\dots,k-1

center

  • The path p1=1,2,4,1p_1 = \langle 1, 2, 4, 1\rangle forms the same cycles as the paths
    • p2=2,4,1,2p_2= \langle 2, 4, 1, 2 \rangle and p3=4,1,2,4p_3= \langle 4, 1, 2, 4\rangle
  • A directed graph with no self-loops is simple
  • In an undirected graph a path v0,v1,,vk\langle v_0,v_1,\dots,v_k\rangle forms a cycle
    • if v0=vkv_0=v_k and v1,v2,,vkv_1,v_2,\dots,v_k are distinct
  • A graph with no cycles is acyclic
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs - Review

  • An undirected graph is connected
    • if every pair of vertices is connected by a path
  • The connected components of a graph are the
    • equivalence classes of vertices under the
    • "is reachable from" relation
  • An undirected graph is connected if it has exactly one component,
    • i.e., if every vertex is reachable from every other vertex
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs - Review

  • A directed graph is strongly-connected
    • if every two vertices are reachable from each other
  • The strongly-connected components of a digraph are the
    • equivalence classes of vertices under the
    • "are mutually reachable" relation
  • A directed graph is strongly-connected
    • if it has only one strongly-connected component
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs - Review

  • Two graphs G=(V,E)G=(V,E) and G=(V,E)G'=(V',E') are isomorphic
    • if there exists a bijection f:VVf : V \rightarrow V' such that
    • (u,v)E    (f(u),f(v))E(u, v) \in E \iff (f (u), f (v)) \in E'
  • That is, we can relabel the vertices of GG to be vertices of GG' maintaining the corresponding edges in GG and GG'

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs - Review

  • A graph G=(V,E)G'=(V', E') is a subgraph of G=(V,E)G=(V,E) if
    • VVV \subseteq V and EEE' \subseteq E
  • Given a set VVV' \subseteq V, the subgraph of GG induced by VV' is the graph
    • G=(V,E)G'=(V', E') where E={(u,v)E:u,vV}E'=\{(u,v) \in E: u,v \in V'\}

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs - Review

  • Given an undirected graph G=(V,E)G=(V,E), the directed version of GG is the directed graph G=(V,E)G'=(V', E'), where
    • (u,v)E(u,v)\in E' and (v,u)E    (u,v)E(v,u) \in E' \iff (u,v) \in E
  • That is, each undirected edge (u,v)(u,v) in GG is replaced in GG' by two directed edges (u,v)(u,v) and (v,u)(v,u)
  • Given a directed graph G=(V,E)G=(V,E), the undirected version of G is the undirected graph G=(V,E)G'=(V',E'), where
    • (u,v)E    uv(u,v) \in E' \iff u \neq v and (u,v)E(u,v) \in E
  • That is the undirected version contains the edges of G
    • "with their directions removed" and with self-loops eliminated
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs - Review

center

  • i.e., (u,v)(u,v) and (v,u)(v,u) in GG are replaced in GG' by the same edge (u,v)(u,v)
  • In a directed graph G=(V,E)G=(V,E), a neighbor of a vertex uu is any vertex that is adjacent to uu in the undirected version of GG
  • That, is vv is a neighbor of u    u \iff either (u,v)E(u,v)\in E or (v,u)E(v,u) \in E

center

  • vv is a neighbor of uu in both cases
  • In an undirected graph, uu and vv are neighbors if they are adjacent
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs - Review

  • Several kinds of graphs are given special names
    • Complete graph: undirected graph in which every pair of vertices is adjacent
    • Bipartite graph: undirected graph G=(V,E)G=(V,E) in which VV can be partitioned into two disjoint sets V1V_1 and V2V_2 such that
      • (u,v)E(u,v) \in E implies either uV1u \in V_1 and vV2v \in V_2 or uV2u \in V_2 and vV1v \in V_1

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs - Review

  • Forest: acyclic, undirected graph
  • Tree: connected, acyclic, undirected graph
  • Dag: directed acyclic graph
  • Multigraph: undirected graph with multiple edges between vertices and self-loops
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Introduction to Graphs - Review

  • Hypergraph: like an undirected graph, but each hyperedge,
    • rather than connecting two vertices,
    • connects an arbitrary subset of vertices

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Free Trees

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Free Trees

  • A free tree is a connected, acyclic, undirected graph
  • We often omit the adjective "free" when we say that a graph is a tree
  • If an undirected graph is acyclic but possibly disconnected it is a forest
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Theorem (Properties of Free Trees)

  • The following are equivalent for an undirected graph G=(V,E)G=(V,E)
  1. GG is a free tree
  2. Any two vertices in GG are connected by a unique simple-path
  3. GG is connected, but if any edge is removed from E the resulting graph is disconnected
  4. GG is connected, and E=V1|E| = |V|-1
  5. GG is acyclic, and E=V1|E| = |V| - 1
  6. GG is acyclic, but if any edge is added to EE, the resulting graph contains a cycle
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (12)(1 \Rightarrow 2)

  1. G is a free tree
  2. Any two vertices in G are connected by a unique simple-path
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (12)(1 \Rightarrow 2)

  • Since a tree is connected, any two vertices in GG are connected by a simple path
    • Let two vertices u,vVu,v \in V are connected by two simple paths p1p_1 and p2p_2
    • Let ww and zz be the first vertices at which p1p_1 and p2p_2 diverge and re-converge
    • Let p1p'_1 be the subpath of p1p_1 from ww to zz
    • Let p2p'_2 be the subpath of p2p_2 from ww to zz
    • p1p'_1 and p2p'_2 share no vertices except their end points
    • The path p1p2p'_1 || p'_2 is a cycle (contradiction)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (12)(1 \Rightarrow 2)

center

  • p1p'_1 and p2p'_2 share no vertices except their end points
  • p1p2p'_1 || p'_2 is a cycle (contradiction)
  • Thus, if GG is a tree, there can be at most one path between two vertices
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (23)(2 \Rightarrow 3)

  1. Any two vertices in GG are connected by a unique simple-path
  2. GG is connected, but if any edge is removed from EE the resulting graph is disconnected
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (23)(2 \Rightarrow 3)

  • If any two vertices in GG are connected by a unique simple path, then GG is connected
    • Let (u,v)(u,v) be any edge in EE. This edge is a path from uu to vv. So it must be the unique path from uu to vv
  • Thus, if we remove (u,v)(u,v) from GG, there is no path from uu to vv
  • Hence, its removal disconnects GG
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (34)(3 \Rightarrow 4)

  • Before proving 343 \Rightarrow 4 consider the following
  • Lemma: any connected, undirected graph G=(V,E)G=(V,E)
    • satisfies EV1|E| \geq |V|-1
  • Proof: Consider a graph GG' with V|V| vertices and no edges.
    • Thus initially there are C=V|C|=|V| connected components
      • Each isolated vertex is a connected component
    • Consider an edge (u,v)(u,v) and let CuC_u and CvC_v denote the connected-components of uu and vv
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (Lemma)$

  • If CuCvC_u \neq C_v then (u,v)(u,v) connects CuC_u and CvC_v into a
    • connected component CuvC_{uv}
  • Otherwise (u,v)(u,v) adds an extra edge to the
    • connected component Cu=CvC_u = C_v
  • Hence, each edge added to the graph reduces the
    • number of connected components by at most 11
  • Thus, at least V1|V|-1 edges are required to reduce the number of components to 11
    • Q.E.DQ.E.D
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (34)(3 \Rightarrow 4)

  1. GG is connected, but if any edge is removed from EE the resulting graph is disconnected
  2. GG is connected, and E=V1|E|=|V|-1
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (34)(3 \Rightarrow 4)

  • By assuming (3), the graph GG is connected
  • We need to show both EV1|E| \geq |V|-1 and EV1|E| \leq |V|-1 in
    • order to show that E=V1|E| = |V|-1
  • EV1|E| \geq |V|-1: valid due previous lemma
  • EV1|E| \leq |V|-1: (proof by induction)
  • Basis: a connected graph with n=1n=1 or n=2n=2 vertices has n1n-1 edges
  • IH: suppose that all graphs G=(V,E)G'=(V',E') satisfying (3) also
    • satisfy EV1|E'| \leq |V'|-1
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (34)(3 \Rightarrow 4)

  • Consider G=(V,E)G=(V,E) that satisfies (3) with V=n3|V|= n \geq 3
  • Removing an arbitrary edge (u,v)(u,v) from GG separates the graph into 2 connected graphs Gu=(Vu,Eu)G_u=(V_u,E_u) and Gv=(Vv,Ev)G_v=(V_v,E_v) such that V=VuVvV = V_u \cup V_v and E=EuEvE=E_u \cup E_v
  • Hence, connected graphs GuG_u and GvG_v both satisfy (3) else GG would not satisfy (3)(3)
  • Note that Vu|V_u| and Vv<n|V_v| < n since Vu+Vv=n|V_u| + |Vv| = n
  • Hence, EuVu1|E_u| \leq |V_u|-1 and EvVv1|E_v| \leq |V_v|-1 (by IH)
  • Thus, E=Eu+Ev+1(Vu1)+(Vv1)+1|E| = |E_u| + |E_v| + 1 \leq (|V_u|-1) + (|V_v|-1) + 1
    • EV1\Rightarrow |E| \leq |V|-1
    • Q.E.DQ.E.D
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (45)(4 \Rightarrow 5)

  1. GG is connected, and E=V1|E|=|V|-1
  2. GG is acyclic, and E=V1|E|=|V|-1
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (45)(4 \Rightarrow 5)

  • Suppose that GG is connected, and E=V1|E|=|V|-1, we must
    • show that GG is acyclic
  • Suppose GG has a cycle containing kk vertices v1,v2,,vkv_1, v_2,\dots, v_k
  • Let Gk=(Vk,Ek)G_k=(V_k,E_k) be subgraph of GG consisting of the cycle

center

  • If k<Vk < |V|, there must be a vertex vk+1VVkv_{k+1} \in V-V_k that is adjacent to some vertex viVkv_i \in V_k, since GG is connected
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (45)(4 \Rightarrow 5)

  • Define Gk+1=(Vk+1,Ek+1)G_{k+1}=(V_{k+1},E_{k+1}) to be subgraph of GG with Vk+1=Vkvk+1V_{k+1}=V_k \cup v_{k+1} and Ek+1=Ek(vk+1,vi)E_{k+1} = E_k \cup (v_{k+1},v_i)

center

  • If k+1<Vk+1 < |V|, we can similarly define Gk+2=(Vk+2,Ek+2)G_{k+2} =(V_{k+2},E_{k+2}) to be the subgraph of GG with
    • Vk+2=Vk+1vk+2V_{k+2}= V_{k+1} \cup v_{k+2} and Ek+2=Ek+1(vk+2,vj)E_{k+2} = E_{k+1} \cup (v_{k+2},v_j)
    • for some vjVk+1v_j \in V_{k+1} where Vk+2=Ek+2|V_{k+2}|=|E_{k+2}|
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (45)(4 \Rightarrow 5)

  • We can continue defining Gk+mG_{k+m} with Vk+m=Ek+m|V_{k+m}|=|E_{k+m}| until we obtain Gn=(Vn,En)G_n = (V_n,E_n) where
    • n=Vn=|V| and Vn=VV_n=|V| and Vn=En=V|V_n|=|E_n|=|V|
  • Since GnG_n is a subgraph of GG, we have
    • EnEEEn=VE_n \subseteq E \Rightarrow |E| \geq |E_n|=|V| which contradicts the assumption E=V1|E|=|V|-1
  • Hence GG is acyclic
    • Q.E.DQ.E.D
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (56)(5 \Rightarrow 6)

  1. GG is acyclic, and E=V1|E| = |V| - 1
  2. GG is acyclic, but if any edge is added to EE, the resulting graph contains a cycle
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (56)(5 \Rightarrow 6)

  • Suppose that GG is acyclic and E=V1|E| = |V| - 1
  • Let kk be the number of connected components of GG
  • G1=(V1,E1),G2=(V2,E2),,Gk=(Vk,Ek)G_1=(V_1,E_1),G_2=(V_2,E_2),\dots,G_k=(V_k,E_k) such that
    • i=1kVi=V;ViVj=;1i,jk\overset{k}{\underset{i=1}{\cup}}V_i=V;V_i \cap V_j = \empty; 1 \leq i,j \leq k and iji \neq j
    • i=1kEi=E;EiEj=;1i,jk\overset{k}{\underset{i=1}{\cup}}E_i=E;E_i \cap E_j = \empty; 1 \leq i,j \leq k and iji \neq j
  • Each connected component GiG_i is a tree by definition.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (56)(5 \Rightarrow 6)

  • Since (15)(1 \Rightarrow 5) each component GiG_i is satisfies
    • Ei=Vi1|E_i| = |V_i| -1 for i=1,2,,ki =1,2, \dots,k
  • Thus
    • i=1kEi=i=1kVii=1k1\sum \limits_{i=1}^{k} |E_i| = \sum \limits_{i=1}^{k} |V_i| - \sum \limits_{i=1}^{k} 1
    • E=Vk|E|=|V|-k
  • Therefore, we must have k=1k=1
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (56)(5 \Rightarrow 6)

  • That is (5)G(5) \Rightarrow G is connected G\Rightarrow G is a tree
  • Since (12)(1 \Rightarrow 2)
    • any two vertices in GG are connected by a unique simple path
  • Thus,
    • adding any edge to GG creates a cycle
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (61)(6 \Rightarrow 1)

  1. GG is acyclic, but if any edge is added to EE, the resulting graph contains a cycle
  2. GG is a free tree
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (61)(6 \Rightarrow 1)

  • Suppose that GG is acyclic but if any edge is added to EE a cycle is created
  • We must show that GG is connected due to the definition
  • Let uu and vv be two arbitrary vertices in GG
  • If uu and vv are not already adjacent
    • adding the edge (u,v)(u,v) creates a cycle in
    • which all edges but (u,v)(u,v) belong to GG
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Properties of Free Trees (61)(6 \Rightarrow 1)

  • Thus there is a path from uu to vv, and since uu and vv are chosen arbitrarily GG is connected

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Elementary Graph Algorithms

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Online Visual Animations

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Online Visual Animations

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Online Visual Animations

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Online Visual Animations

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Tools

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Tools

  • Graphviz Tools
  • Graphviz (short for Graph Visualization Software) is a package of open-source tools initiated by AT&T Labs Research for drawing graphs specified in DOT language scripts having the file name extension "gv". It also provides libraries for software applications to use the tools. Graphviz is free software licensed under the Eclipse Public License.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Tools

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz Gallery

Family Tree

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz Gallery

Neural Network (Keras)

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz Tools and Binaries

  • Graphviz consists of a graph description language named the DOT language[4] and a set of tools that can generate and/or process DOT files:
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz Layout Engines

dot

a command-line tool to produce layered drawings of directed graphs in a variety of output formats, such as (PostScript, PDF, SVG, annotated text and so on).

Visit: https://graphviz.org/docs/layouts/dot/

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz Layout Engines

neato

useful for undirected graphs. "spring model" layout, minimizes global energy. Useful for graphs up to about 1000 nodes

Visit : https://graphviz.org/docs/layouts/neato/

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz Layout Engines

fdp

useful for undirected graphs. "spring model" which minimizes forces instead of energy

Visit : https://graphviz.org/docs/layouts/fdp/

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz Layout Engines

sfdp

multiscale version of fdp for the layout of large undirected graphs

Visit : https://graphviz.org/docs/layouts/sfdp/

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz Layout Engines

twopi

for radial graph layouts. Nodes are placed on concentric circles depending their distance from a given root node

Visit : https://graphviz.org/docs/layouts/twopi/

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz Layout Engines

circo

circular layout. Suitable for certain diagrams of multiple cyclic structures, such as certain telecommunications networks

Visit : https://graphviz.org/docs/layouts/circo/

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz Layout Engines

osage

osage draws clustered graphs. Suitable for certain diagrams of multiple cyclic structures, such as certain telecommunications networks

Visit : https://graphviz.org/docs/layouts/osage/

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz Layout Engines

patchwork

patchwork draws clustered graphs using a squarified treemap layout.

Visit : https://graphviz.org/docs/layouts/patchwork/

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz Layout Engines

dotty (DEPRECATED)

a graphical user interface to visualize and edit graphs.

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz Tools

lefty (DEPRECATED)

a programmable (in a language inspired by EZ[5]) widget that displays DOT graphs and allows the user to perform actions on them with the mouse. Therefore, Lefty can be used as the view in a model–view–controller GUI application that uses graphs.

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz Tools

gml2gv - gv2gml

convert to/from GML, another graph file format.

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz Tools

graphml2g

convert a GraphML file to the DOT format.

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz Tools

gxl2gv - gv2gxl

convert to/from GXL, another graph file format.

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz Tools

for more information visit

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graphviz API

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Tools

  • Plantuml Tools (https://plantuml.com/download)
    • PlantUML is an open-source tool allowing users to create diagrams from a plain text language. Besides various UML diagrams, PlantUML has support for various other software development related formats (such as Archimate, Block diagram, BPMN, C4, Computer network diagram, ERD, Gantt chart, Mind map, and WBD), as well as visualisation of JSON and YAML files.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Tools

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Tools

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Tools

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Tools

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Elementary Graph Algorithms

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Elementary Graph Algorithms

  • Graph Traversal

    • Breadth-first search (BFS)
    • Depth-first search (DFS)
  • Strongly connected components (SCC)

    • Kosaraju's algorithm
    • Tarjan's algorithm
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Elementary Graph Algorithms

  • Topological sort

    • DFS version
    • BFS version (Kahn's algorithm)
  • Minimum spanning tree

    • Kruskal's algorithm
    • Prim's algorithm
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Elementary Graph Algorithms

  • Cycle Detection
    • DFS
    • BFS
  • Bipartite Graph Check
    • DFS
    • BFS
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Breadth-first search (BFS)

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS)

  • Breadth-first search (BFS) is a graph traversal algorithm that starts at a vertex and explores as far as possible along each branch before backtracking.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS)

  • Graph G=(V,E)G=(V, E), directed or undirected with adjacency list repres.
  • GOAL: Systematically explores edges of G to
    • discover every vertex reachable from the source vertex ss
    • compute the shortest path distance of every vertex
      • from the source vertex ss
    • produce a breadth-first tree (BFT) GπG_\pi with root ss
      • BFT contains all vertices reachable from ss
      • the unique path from any vertex vv to ss in G constitutes a shortest path from ss to vv in GG
  • IDEA: Expanding frontier across the breadth -greedy-
    • propagate a wave 11 edge-distance at a time
    • using a FIFO queue: O(1)O(1) time to update pointers to both ends
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS)

  • Maintains the following fields for each uVu \in V
    • color[u]:color[u]: color of uu
      • WHITEWHITE : not discovered yet
      • GRAYGRAY : discovered and to be or being processed
      • BLACKBLACK : discovered and processed
    • π[u]\pi[u]: parent of uu (NILNIL of u=su = s or uu is not discovered yet)
    • d[u]d[u]: distance of uu from ss

Processing a vertex=scanning its adjacency listProcessing \ a \ vertex = scanning \ its \ adjacency \ list

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS) Algorithm

BFS(G,s)for each uVsdocolor[u]WHITEπ[u]NIL;d[u]color[s]GRAYπ[s]NIL;d[s]0Qswhile Q douhead[Q]for each v in Adj[u] doif color[v]WHITE thencolor[v]GRAYπ[v]ud[v]d[u]+1ENQUEUE(Q,v)DEQUEUE(Q)color[u]BLACK\begin{align*} & BFS(G, s) \\ & \quad for \ each \ u \in V - {s} do \\ & \qquad color[u] \rightarrow WHITE \\ & \qquad \pi[u] \rightarrow NIL; d [u] \rightarrow \infty \\[8pt] & \quad color[s] \rightarrow GRAY \\ & \quad \pi[s] \rightarrow NIL; d [s] \rightarrow 0 \\ & \quad Q \rightarrow {s} \\[8pt] & \quad while \ Q \neq \empty \ do \\ & \qquad u \rightarrow head[Q] \\ & \qquad for \ each \ v \ in \ Adj[u] \ do \\ & \quad \qquad if \ color[v] \rightarrow WHITE \ then \\ & \qquad \qquad color[v] \rightarrow GRAY \\ & \qquad \qquad \pi[v] \rightarrow u \\ & \qquad \qquad d [v] \rightarrow d [u] + 1 \\ & \qquad \qquad ENQUEUE(Q, v) \\ & \qquad DEQUEUE(Q) \\ & \qquad color[u] \rightarrow BLACK \end{align*}

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS)

  • In this algorithm, we use a queue to store the vertices that are yet to be visited.

  • Complexity of following part is O(V)O(V)

    G -> Graph
    s -> Source
    BFS(G,s)
      // Mark all the vertices as not visited
      for each vertex u in G.V - {s}
          u.color = white;
          u.distance = infinity;
          u.parent = NIL;
    ...
    
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS)

  • We enqueue the first vertex and mark it as visited.

  • Complexity of following part is O(1)O(1)

    ...
      s.color = gray;
      s.distance = 0;
      s.parent = NIL;
      // Create a queue for BFS
      Q = empty
      ENQUEUE(Q, s)
    ...
    
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS)

  • We dequeue a vertex u and mark it as visited.

  • We enqueue all the adjacent vertices of u.

  • Complexity of following part is O(E)O(E)

    ...
      WHILE Q is not empty
          u = DEQUEUE(Q)
          for each vertex v in G.Adj[u]
              if v.color == white
                  v.color = gray;
                  v.distance = u.distance + 1;
                  v.parent = u;
                  ENQUEUE(Q, v)
          u.color = black;
    
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS)

  • Complexity of BFS is O(V+E)=O(V)+O(E)+O(1)O(V+E) = O(V)+O(E)+O(1)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS) Complete Algorithm

G -> Graph
s -> Source
BFS(G,s)
    // Mark all the vertices as not visited
    for each vertex u in G.V - {s}
        u.color = white;
        u.distance = infinity;
        u.parent = NIL;
    s.color = gray;
    s.distance = 0;
    s.parent = NIL;
    // Create a queue for BFS
    Q = empty
    ENQUEUE(Q, s)
    WHILE Q is not empty
        u = DEQUEUE(Q)
        for each vertex v in G.Adj[u]
            if v.color == white
                v.color = gray;
                v.distance = u.distance + 1;
                v.parent = u;
                ENQUEUE(Q, v)
        u.color = black;
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS) Example-1

  • s is the source vertex.

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS) Example-1

  • STEP-1

    //init the graph
    s.parent = NIL;
    s.color = gray;
    s.distance = 0;
    Q = empty;
    ENQUEUE(Q, s)
    and 
    u = DEQUEUE(Q) in the while loop
    
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS) Example-1

  • STEP-2
----------------
Q = {c,a}
s = b
----------------
c.parent = s
c.distance = 1
c.color = gray
----------------
a.parent = s
a.distance = 1
a.color = gray
----------------
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS) Example-1

  • STEP-3
----------------
Q = {e,c}
a = b
----------------
e.parent = a
e.distance = 2
e.color = gray
----------------
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS) Example-1

  • STEP-4
----------------
Q = {g,e}
c = b
----------------
g.parent = c
g.distance = 2
g.color = gray
----------------
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS) Example-1

  • STEP-5
----------------
Q = {b,h,g}
e = b
----------------
h.parent = e
h.distance = 3
h.color = gray
----------------
b.parent = e
b.distance = 3
b.color = gray
----------------
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS) Example-1

  • STEP-6
----------------
Q = {f,i,b,h}
g = b
----------------
i.parent = g
i.distance = 3
i.color = gray
----------------
f.parent = e
f.distance = 3
f.color = gray
----------------
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS) Example-1

  • STEP-7
----------------
Q = {f,i,b}
h = b
----------------
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS) Example-1

  • STEP-8
----------------
Q = {f,i}
b = b
----------------
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS) Example-1

  • STEP-9
----------------
Q = {f}
i = b
----------------
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS) Example-1

  • STEP-10
----------------
Q = {}
f = b
----------------
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS) Example-1

  • BFS is done and the graph is traversed.
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS) Print Paths

  • Prints out vertices on a svs \rightarrow v shortest path

PRINT-PATH(G,s,v)if v=s then print selse if[v]=NIL thenprint no "sv path"elsePRINT-PATH(G,s,[v])print v\begin{align*} & \text{PRINT-PATH}(G, s, v) \\ & \quad if \ v = s \ then \ print \ s \\ & \quad else \ if \prod[v] = NIL \ then \\ & \qquad print \ no \ "s \rightarrow v \ path" \\ & \quad else \\ & \qquad \text{PRINT-PATH}(G, s, \prod[v] ) \\ & \qquad print \ v \end{align*}

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS) Algorithm Summary

  • Step 1 - Define a Queue of size total number of vertices in the graph.
  • Step 2 - Select any vertex as starting point for traversal. Visit that vertex and insert it into the Queue.
  • Step 3 - Visit all the non-visited adjacent vertices of the vertex which is at front of the Queue and insert them into the Queue.
  • Step 4 - When there is no new vertex to be visited from the vertex which is at front of the Queue then delete that vertex.
  • Step 5 - Repeat steps 3 and 4 until queue becomes empty.
  • Step 6 - When queue becomes empty, then produce final spanning tree by removing unused edges from the graph
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-first search (BFS) Running Time

  • Running time: O(V+E)=O(V+E) = considered linear time in graphs
    • initialization: Θ(V)\Theta(V)
    • queue operations: O(V)O(V)
      • each vertex enqueued and dequeued at most once
      • both enqueue and dequeue operations take O(1)O(1) time
    • processing gray vertices: O(E)O(E)
      • each vertex is processed at most once and
      • uVAdj[u]=Θ(E)\sum \limits{u \in V}{}|Adj[u]|=\Theta(E)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

BeginingofBFSProofBegining-of-BFS-Proof

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Theorems Related to BFS

  • DEF: δ(s,v)=\delta(s, v) = shortest path distance from ss to vv
  • LEMMA 1: for any sV&(u,v)E;δ(s,v)δ(s,u)+1s \in V \& (u, v) \in E; \delta(s, v) \leq \delta(s, u) + 1
  • For any BFS(G,s)BFS(G, s) run on G=(V,E)G=(V,E)
  • LEMMA 2: d[v]δ(s,v)  vVd[v] \geq \delta(s, v) \ \forall \ v \in V
  • LEMMA 3: at any time of BFSBFS, the queue Q=v1,v2,,vrQ=\langle v_1, v_2, \dots, v_r \rangle satisfies
    • d[vr]d[v1]+1d[v_r] \leq d[v_1] + 1
    • d[vi]d[vi+1], for i=1,2,,r1d[v_i] \leq d[v_{i+1}], \ for \ i = 1, 2, \dots, r - 1
  • THM1: BFS(G,s)BFS(G, s) achieves the following
    • discovers every vVv \in V where svs \rightarrow v (i.e., vv is reachable from ss)
    • upon termination, d[v]=δ(s,v)  vVd[v]= \delta(s, v) \ \forall \ v \in V
    • for any vs&sv;sp(s,[v])([v],v)v \neq s \& s \rightarrow v; sp(s, \prod[v]) \sim (\prod[v], v) is a sp(s,v)sp(s, v)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Proofs of BFS Theorems

  • DEF: shortest path distance δ(s,v)\delta(s, v) from ss to vv

    • δ(s,v)=\delta(s, v)= minimum number of edges in any path from ss to vv
    • == \infty if no such path exists (i.e., vv is not reachable from ss)
  • L1: for any sV&(u,v)E;δ(s,v)δ(s,u)+1s \in V \& (u, v) \in E; \delta(s, v) \leq \delta(s, u) + 1

  • PROOF: susvs \rightarrow u \Rightarrow s \rightarrow v. Then,

    • consider the path p(s,v)=sp(s,u)(u,v)p(s, v) = sp(s, u) \sim (u, v)
    • p(s,v)=sp(s,u)+1=δ(s,u)+1|p(s, v)| = | sp(s, u) | + 1 = \delta(s, u) + 1
    • therefore, δ(s,v)p(s,v)=δ(s,u)+1\delta(s, v) \leq |p(s, v)| = \delta(s, u) + 1
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Proofs of BFS Theorems

  • DEF: shortest path distance δ(s,v)\delta(s, v) from ss to vv

  • δ(s,v)=\delta(s, v) = minimum number of edges in any path from s to v

  • L1: for any sV&(u,v)E;δ(s,v)δ(s,u)+1s \in V \& (u, v) \in E; \delta(s, v) \leq \delta(s, u) + 1

  • C1 of L1: if G=(V,E)G=(V,E) is undirected then (u,v)E(v,u)E(u, v) \in E \Rightarrow (v, u) \in E

    • δ(s,v)δ(s,u)+1\delta(s, v) \leq \delta(s, u) + 1 and δ(s,u)δ(s,v)+1\delta(s, u) \leq \delta(s, v) + 1
    • δ(s,u)1δ(s,v)δ(s,u)+1\Rightarrow \delta(s, u) - 1 \leq \delta(s, v) \leq \delta(s, u) + 1 and
    • δ(s,v)1δ(s,u)δ(s,v)+1\delta(s, v) - 1 \leq \delta(s, u) \leq \delta(s, v) + 1
    • δ(s,u) & δ(s,v)\Rightarrow \delta(s, u) \ \& \ \delta(s, v) differ by at most 11
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Proofs of BFS Theorems

  • L2: upon termination of BFS(G,s)BFS(G, s) on G=(V,E)G=(V,E);
    • d[v]δ(s,v)  vVd[v] \geq \delta(s, v) \ \forall \ v \in V
  • PROOF: by induction on the number of ENQUEUE operations
    • basis: immediately after 1st enqueue operation
      • ENQ(Q,s):d[s]=δ(s,s)ENQ(Q, s): d[s] = \delta(s, s)
    • hypothesis: d[v]δ(s,v)d [v] \geq \delta(s, v) for all vv inserted into QQ
    • induction: consider a white vertex vv discovered during scanning Adj[u]Adj[u]
      • d[v]=d[u]+1d [v] = d [u] + 1 due to the assignment statement
      • δ(s,u)+1\geq \delta(s, u) + 1 due to the inductive hypothesis since uQu \in Q
      • δ(s,v)\geq \delta(s, v) due to L1L1
    • vertex vv is then enqueued and it is never enqueued again
      • d[v]d[v] never changes again, maintaining inductive hypothesis
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Proofs of BFS Theorems

  • L3: Let Q=v1,v2,,vrQ = \langle v_1,v_2,\dots, v_r\rangle during the execution of BFS(G,s)BFS(G, s), then,

    • d[vr]d[v1]+1d[v_r] \leq d[v_1] + 1 and d[vi]d[vi+1]d[v_i] \leq d[v_{i+1}] for i=1,2,,r1i=1,2,\dots,r-1
  • PROOF: by induction on the number of QUEUEQUEUE operations

    • basis: lemma holds when QsQ \leftarrow {s}
    • hypothesis: lemma holds for a particular QQ (i.e., after a certain #\# of QUEUEQUEUE operations)
    • induction: must prove lemma holds after both DEQUEUEDEQUEUE & ENQUEUEENQUEUE operations
    • DEQUEUE(Q):Q=v1,v2,,vrQ=v2,v3,,vrDEQUEUE(Q): Q = \langle v_1, v_2, \dots , v_r \rangle \Rightarrow Q' = \langle v_2, v_3,\dots, v_r \rangle
      • d[vr]d[v1]+1d[v_r] \leq d[v_1] + 1 & d[v1]d[v2]d[v_1] \leq d[v_2] in Qd[vr]d[v2]+1Q \Rightarrow d[v_r] \leq d[v_2]+1 in QQ'
      • d[vi]d[vi+1]d[v_i] \leq d[v_{i+1}] for i=1,2,,r1i=1,2,\dots,r-1 in QQ'
      • d[vi]d[vi+1]d[v_i] \leq d[v_{i+1}] for i=2,,r1i=2,\dots,r-1 in QQ'
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Proofs of BFS Theorems

  • ENQUEUE(Q,v):ENQUEUE(Q, v):
    • Q=v1,v2,,vrQ = \langle v_1, v_2, \dots , v_r \rangle \Rightarrow
    • Q=v1,v2,,vr,vr+1=vQ' = \langle v_1, v_2,\dots, v_r,v_{r+1} = v \rangle
  • vv was encountered during scanning Adj[u]Adj[u] where u=v1u = v_1
  • thus, d[vr+1]=d[v]=d[u]+1=d[v1]+1d [v_{r+1}] = d[v] = d [u] + 1 = d [v_1] + 1 \Rightarrow
    • d[vr+1]=d[v1]+1d[v_{r+1}] = d [v_1] + 1 in QQ'
  • but d[vr]d[v1]+1=d[vr+1]d[v_r] \leq d[v_1] + 1 = d [v_{r+1}]
    • d[vr+1]=d[v1]+1\Rightarrow d[v_{r+1}] = d[v_1] + 1 and d[vr]d[vr+1]d [v_r] \leq d[v_{r+1}] in QQ'
  • C3 of L3 (monotonicity property):
    • if: the vertices are enqueued in the order v1,v2,,vnv_1, v_2, \dots, v_n
    • then: the sequence of distances is monotonically increasing,
      • i.e., d[v1]d[v2]d[vn]d [v_1] \leq d [v_2] \leq \dots \leq d[v_n]
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Proofs of BFS Theorems

  • THM (correctness of BFS): BFS(G,s)BFS(G, s) achieves the following on G=(V,E)G=(V,E)

    • discovers every vVv \in V where svs \rightarrow v
    • upon termination: d[v]=δ(s,v) vVd[v] = \delta(s, v) \ \forall v \in V
    • for any vs&sv;sp(s,[v])([v],v)=sp(s,v)v \neq s \& s \rightarrow v; sp(s,\prod[v]) \sim (\prod[v], v) = sp(s, v)
  • PROOF: by induction on kk, where Vk={vV:δ(s,v)=k}V_k = \{v \in V: \delta(s, v) = k \}

    • hypothesis: for each vVk,v \in V_k, \exists exactly one point during execution of BFS at which color[v]GRAY,d[v]k,[v]uVk1color[v] \rightarrow GRAY, d [v] \rightarrow k, \prod[v] \rightarrow u \in V_{k-1}, and then ENQUEUE(Q,v)ENQUEUE(Q, v)
    • basis: for k=0k=0 since V0={s};color[s]GRAY,d[s]0V_0 = \{s\}; color[s] \rightarrow GRAY, d[s] \rightarrow 0 and ENQUEUE(Q,s)ENQUEUE(Q, s)
    • induction: must prove hypothesis holds for each vVk+1v \in V_{k+1}
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Proofs of BFS Theorems

  • Consider an arbitrary vertex vVk+1v \in V_{k+1}, where k0k \geq 0
    • monotonicity (L3)+d[v]k+1 (L2)+(L3) + d[v] \geq k+1 \ (L2)+ inductive hypothesis
      • \Rightarrow vv must be discovered after all vertices in VkV_k were enqueued
    • since δ(s,v)=k+1, uVk\delta(s, v) = k + 1, \exists \ u \in V_k such that (u,v)E(u, v) \in E
    • let uVku \in V_k be the first such vertex grayed (must happen due to hyp.)
    • uhead(Q)u \leftarrow head(Q) will be ultimately executed since BFSBFS enqueues every grayed vertex
      • vv will be discovered during scanning Adj[u]Adj[u]
        • color[v]WHITEcolor[v] \leftarrow WHITE since vv isn’t adjacent to any vertex in VjV_j for j<kj<k
      • color[v]GRAYcolor[v] \leftarrow GRAY, d[v]d[u]+1,[v]ud[v] \leftarrow d[u]+ 1, \prod[v] \leftarrow u
      • then, ENQUEUE(Q,v)ENQUEUE(Q, v) thus proving the inductive hypothesis
  • To conclude the proof
    • if vVk+1v \in V_{k+1} then due to above inductive proof [v]Vk\prod[v] \in V_k
    • thus sp(s,[v])([v],v)sp(s, \prod[v]) \sim (\prod[v], v) is a shortest path from ss to vv
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Theorems Related to BFS

  • DEF: δ(s,v)=\delta(s, v) = shortest path distance from ss to vv
  • LEMMA 1: for any sV&(u,v)E;δ(s,v)δ(s,u)+1s \in V \& (u, v) \in E; \delta(s, v) \leq \delta(s, u) + 1
  • For any BFS(G,s)BFS(G, s) run on G=(V,E)G=(V,E)
  • LEMMA 2: d[v]δ(s,v)vVd[v] \geq \delta(s, v) \forall v \in V
  • LEMMA 3: at any time of BFSBFS, the queue Q=v1,v2,,vrQ=\langle v_1, v_2,\dots, v_r \rangle satisfies
    • d[vr]d[v1]+1d[v_r] \leq d[v_1] + 1
    • d[vi]d[vi+1], for i=1,2,,r1d[v_i] \leq d[v_{i+1}], \ for \ i=1,2,\dots,r-1
  • THM1: BFS(G,s)BFS(G, s) achieves the following
    • discovers every vVv \in V where svs \rightarrow v (i.e., vv is reachable from ss)
    • upon termination, d[v]=δ(s,v)vVd[v]= \delta(s, v) \forall v \in V
    • for any vs&sv;sp(s,[v])([v],v)v \neq s \& s \rightarrow v; sp(s, \prod[v]) \sim (\prod[v], v) is a sp(s,v)sp(s, v)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Breadth-First Tree Generated by BFS

  • LEMMA 4: predecessor subgraph G=(V,E)G_{\prod}=(V_{\prod}, E_{\prod}) generated by BFS(G,s)BFS(G, s), where
    • V={vV:[v]NIL}sV_{\prod} =\{v \in V: \prod[v] \neq NIL\}\cup{s} and
    • E={([v],v)E:vV{s}}E_{\prod} =\{(\prod[v],v) \in E: v \in V_{\prod} -\{s\}\}
  • is a breadth-first tree such that
    • VV_{\prod} consists of all vertices in VV that are reachable from ss
    • vV\forall v \in V_{\prod}, unique path p(v,s)p(v, s) in GG_{\prod} constitutes a sp(s,v)sp(s, v) in GG
RTEU CE100 Week-10
CE100 Algorithms and Programming II

EndofBFSProofEnd-of-BFS-Proof

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Depth-first search (DFS)

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS)

  • DFS is a traversal algorithm that visits each vertex in a graph in a depth-first manner.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS)

  • Graph G=(V,E)G=(V,E) directed or undirected
  • Adjacency list representation
  • Goal: Systematically explore every vertex and every edge
  • Idea: search deeper whenever possible
    • Using a LIFO queue (Stack; FIFO queue used in BFS)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS)

  • Maintains several fields for each vVv \in V
  • Like BFSBFS, colors the vertices to indicate their states. Each vertex is
    • Initially whitewhite,
    • grayedgrayed when discovered,
    • blackenedblackened when finished
  • Like BFSBFS, records discovery of a white vv during scanning Adj[u]Adj[u] by π[v]u\pi[v] \rightarrow u
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS)

  • Unlike BFSBFS, predecessor graph GπG_{\pi} produced by DFS forms spanning forest
  • Gπ=(V,Eπ)G_{\pi}=(V,E_{\pi}) where
    • Eπ={(π[v],v):vVandπ[v]NIL}E_{\pi}=\{(\pi[v],v): v \in V \text{and} \pi[v] \neq NIL\}
  • Gπ=G_{\pi}= depth-first forest (DFF) is composed of disjoint depth-first trees (DFTs)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS)

  • DFS also timestamps each vertex with two timestamps
  • d[v]d[v]: records when v is first discovered and grayed
  • f[v]f[v]: records when v is finished and blackened
  • Since there is only one discovery event and finishing event for each vertex we have 1d[v]f[v]2V1\leq d[v] \leq f[v] \leq 2|V|
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Algorithm

DFS(G)for each uV docolor[u]whiteπ[u]NILtime0for each uV doif color[u]=white thenDFS-VISIT(G,u)\begin{align*} & \text{DFS}(G) \\ & \quad for \ each \ u \in V \ do \\ & \qquad color[u] \leftarrow white \\ & \qquad \pi[u] \leftarrow NIL \\[8pt] & \quad time \leftarrow 0 \\[8pt] & \quad for \ each \ u \in V \ do \\ & \qquad if \ color[u] = white \ then \\ & \quad \qquad \text{DFS-VISIT}(G, u) \end{align*}

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Algorithm

DFS-VISIT(G,u)color[u]grayd[u]timetime+1for each vAdj[u] doif color[v]=white thenπ[v]uDFS-VISIT(G,v)color[u]blackf[u]timetime+1\begin{align*} & \text{DFS-VISIT}(G, u) \\ & \quad color[u] \leftarrow gray \\ & \quad d[u] \leftarrow time \leftarrow time +1 \\[8pt] & \quad for \ each \ v \in Adj[u] \ do \\ & \qquad if \ color[v] = white \ then \\ & \quad \qquad \pi[v] \leftarrow u \\ & \quad \qquad \text{DFS-VISIT}(G, v) \\[8pt] & \quad color[u] \leftarrow black \\ & \quad f[u] \leftarrow time \leftarrow time+1 \end{align*}

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Algorithm

  • Complexity of the following part is Θ(V+V)=O(V)\Theta(V+V)=O(V) (two sequential loops)
DFS(G)
 for each vertex u in G.V
    u.color = white
    u.parent = nil
 time = 0
  for each vertex u in G.V
    if u.color == white
      DFS-VISIT(G,u)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Algorithm

DFS-VISIT(G,u)
  time = time + 1
  u.discovery = time
  u.color = gray
  for each vertex v in G.Adj[u]
    if v.color == white
      v.parent = u
      DFS-VISIT(G,v)
  u.color = black
  time = time + 1
  u.finish = time
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS)

  • DFS complexity is Θ(V+E)\Theta(V+E)
  • Note for all vv.discovery<v.finishv \rightarrow v.discovery < v.finish
    • 1u.discovery<u.finish2V1 \leq u.discovery < u.finish \leq 2|V|
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Edge Classification in a DFF

  • Edge Types in DFS
    • Tree Edges
    • Back Edges
    • Forward Edges
    • Cross Edges
  • Colors in DFS
    • White -> Tree Edges
    • Gray -> Back Edges
    • Black -> Forward Edges
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Edge Classification in a DFF

  • Tree Edge: discover a new (WHITE) vertex
    • GRAYWHITEGRAY \Rightarrow WHITE
  • Back Edge: from a descendent to an ancestor in DFT
    • GRAYGRAYGRAY \Rightarrow GRAY
  • Forward Edge: from ancestor to descendent in DFT
    • GRAYBLACKGRAY \Rightarrow BLACK
  • Cross Edge: remaining edges (btwn trees and subtrees)
    • GRAYBLACKGRAY \Rightarrow BLACK
  • Note: ancestor/descendent is wrt Tree Edges
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Edge Classification in a DFF

  • How to decide which GRAYGRAY to BLACKBLACK edges are forward, which are cross
    • Let BLACKBLACK vertex vAdj[u]v \in Adj[u] is encountered while processing GRAYGRAY vertex uu
      • (u,v)(u,v) is a forward edge if d[u]<d[v]d[u] < d[v]
      • (u,v)(u,v) is a cross edge if d[u]<d[v]d[u] < d[v]
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

  • STEP-1
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

  • STEP-2
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

  • STEP-3
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

  • STEP-4
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

  • STEP-5
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

  • STEP-6
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

  • STEP-7
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

  • STEP-8
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

  • STEP-9
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

  • STEP-10
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

  • STEP-11
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

  • STEP-12
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

  • STEP-13
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

  • STEP-14
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

  • STEP-15
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

  • FINAL STEP-16
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-1

  • Edges and Clusters after DFS
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS) Example-2

  • Different Start Point and Different Graph
    center
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Depth-first search (DFS)

  • Running time: Θ(V+E)\Theta(V+E)
  • Initialization loop in DFSDFS : Θ(V)\Theta(V)
  • Main loop in DFS\text{DFS}: Θ(V)\Theta(V) exclusive of time to execute calls to DFS-VISIT\text{DFS-VISIT}
  • DFS-VISIT\text{DFS-VISIT} is called exactly once for each vVv\in V since
  • DFS-VISIT\text{DFS-VISIT} is invoked only on white vertices and
  • DFS-VISIT(G,u)\text{DFS-VISIT}(G, u) immediately colors u as gray
  • For loop of DFS-VISIT(G,u)\text{DFS-VISIT}(G, u) is executed Adj[u]|Adj[u]| time
  • Since Adj[u]=E\sum |Adj[u]| = E, total cost of executing loop of
    • DFS-VISIT\text{DFS-VISIT} is Θ(E)\Theta(E)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Elementary Graph Algorithms

Depth-first search (DFS) Algorithm Summary

  • Step 1 - Define a Stack of size total number of vertices in the graph.
  • Step 2 - Select any vertex as starting point for traversal. Visit that vertex and push it on to the Stack.
  • Step 3 - Visit any one of the non-visited adjacent vertices of a vertex which is at the top of stack and push it on to the stack.
  • Step 4 - Repeat step 3 until there is no new vertex to be visited from the vertex which is at the top of the stack.
  • Step 5 - When there is no new vertex to visit then use back tracking and pop one vertex from the stack.
  • Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
  • Step 7 - When stack becomes Empty, then produce final spanning tree by removing unused edges from the graph
RTEU CE100 Week-10
CE100 Algorithms and Programming II

BeginingofDFSProofBegining-of-DFS-Proof

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Elementary Graph Algorithms

DFS: Parenthesis Theorem

  • Thm: In any DFS of G=(V,E)G=(V,E), let int[v]=[d[v],f[v]]int[v] = [d[v], f[v]] then exactly one of the following holds
  • for any uu and vVv \in V
    • int[u]int[u] and int[v]int[v] are entirely disjoint
    • int[v]int[v] is entirely contained in int[u]int[u] and
      vv is a descendant of uu in a DFTDFT
    • int[u]int[u] is entirely contained in int[v]int[v] and
      uu is a descendant of vv in a DFTDFT
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Elementary Graph Algorithms

Parenthesis Thm (proof for the case d[u]<d[v]d[u] < d[v])

  • Subcase d[v]<f[u]d[v] < f[u] (int[u]int[u] and int[v]int[v] are overlapping)
    • vv was discovered while uu was still GRAYGRAY
    • This implies that vv is a descendant of uu
    • So search returns back to uu and finishes uu after finishing vv
    • i.e., d[v]<f[u]int[v]d[v] < f[u] \Rightarrow int[v] is entirely contained in int[u]int[u]
  • Subcase d[v]>f[u]int[v]d[v] > f[u] \Rightarrow int[v] and int[u]int[u] are entirely disjoint
  • Proof for the case d[v]<d[u]d[v] < d[u] is similar (dual)
    Q.E.DQ.E.D
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Elementary Graph Algorithms

Nesting of Descendents’ Intervals

  • Corollary 1 (Nesting of Descendents’ Intervals):
    • vv is a descendant of u if and only if
      • d[u]<d[v]<f[v]<f[u]d[u] < d[v] < f[v] < f[u]
  • Proof: immediate from the Parenthesis Thrm
    Q.E.DQ.E.D
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Elementary Graph Algorithms

DFS Parenthesis Theorem

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Elementary Graph Algorithms

DFS on Undirected Graphs

  • Ambiguity in edge classification, since (u,v)(u,v) and (v,u)(v,u) are the same edge
    First classification is valid (whichever of (u,v)(u,v) or (v,u)(v,u) is explored first)
    Lemma 1: any DFSDFS on an undirected graph produces only TreeTree and BackedgesBack edges
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Elementary Graph Algorithms

DFS on Undirected Graphs - Lemma 1: Proof

  • Assume (x,z)(x,z) is a F(F?)F(F?) (Figure-1)
    • But (x,z)(x,z) must be a BB, since DFSDFS must finish zz before resuming xx
  • Assume (u,v)(u,v) is a C(C?)C(C?) btw subtrees (Figure-2-4)
    • But (y,u)&(y,v)(y,u) \& (y,v) cannot be both TT; one must be a BB and (u,v)(u,v) must be a TT
  • If (u,v)(u,v) is first explored while processing u/v,(y,v)/(y,u)u /v, (y,v) / (y,u) must be a BB (Figure-2-4)

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Elementary Graph Algorithms

DFS on Undirected Graphs

  • Lemma 2: an undirected graph is acyclic (i.e. a forest) iff DFS yields no BackedgesBack edges
  • Proof
    • (acyclic \Rightarrow no Back edges; by contradiction):
      • Let (u,v)(u,v) be a BB then color[u]=color[v]=GRAYcolor[u] = color[v] = GRAY
        • \Rightarrow there exists a path between uu and vv
      • So, (u,v)(u,v) will complete a cycle (BackedgecycleBack edge \Rightarrow cycle)
  • (noBackedgesacyclicno Back edges \Rightarrow acyclic):
    • If there are no BackedgesBack edges then there are only TT edges by
    • Lemma 1 \Rightarrow forest \Rightarrow acyclic
      Q.E.DQ.E.D
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Elementary Graph Algorithms

DFS on Undirected Graphs (Cycle Detection)

  • How to determine whether an undirected graph G=(V,E)G=(V,E) is acyclic
    • Run a DFSDFS on GG:
      • if a BackedgeBack edge is found then there is a cycle
    • Running time: O(V)O(V), not O(V+E)O(V + E)
    • If ever seen V|V| distinct edges,
      • must have seen a back edge (EV1|E| \leq |V|-1 in a forest)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

DFS: White Path Theorem

  • WPT: In a DFSDFS of GG, vv is a descendent of uu iff at time d[u]d[u], vv can be reached from uu along a WHITEWHITE path
  • Proof (\Rightarrow): assume vv is a descendent of uu
    • Let ww be any vertex on the path from uu to vv in the DFTDFT
      • So, ww is a descendent of ud[u]<d[w]u \Rightarrow d[u] < d[w]
        • (by Corollary 1 nesting of descendents’ intervals)
      • Hence, ww is white at time d[u]d[u]
RTEU CE100 Week-10
CE100 Algorithms and Programming II

DFS: White Path Theorem

  • Proof (\Leftarrow) assume a white path p(u,v)p(u,v) at time d[u]d[u] but vv does not become a descendent of uu in the DFTDFT (contradiction):
    • Assume every other vertex along pp becomes a descendent of uu in the DFTDFT

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

DFS: White Path Theorem

  • otherwise let vv be the closest vertex to uu along pp that does
    • not become a descendent
  • Let w be predecessor of vv along p(u,v)p(u,v):
    • d[u]<d[w]<f[w]<f[u]d[u] < d[w] < f[w] < f[u] by Corollary 1
      • Since, vv was WHITEWHITE at time d[u]d[u] (uu was GRAYGRAY) d[u]<d[v]d[u] < d[v]
      • Since, ww is a descendent of uu but vv is not
        • d[w]<d[v]d[v]<f[w]d[w] < d[v] \Rightarrow d[v] < f[w]
  • By (1)–(3): d[u]<d[v]<f[w]<f[u]d[u]<d[v]<f[w]d[u] < d[v] < f[w] < f[u] \Rightarrow d[u] < d[v] < f[w]
  • So by Parenthesis Thm int[v]int[v] is within int[u]int[u], vv is descendent of uu

Q.E.DQ.E.D

RTEU CE100 Week-10
CE100 Algorithms and Programming II

EndofDFSProofEnd-of-DFS-Proof

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Strongly Connected Components (SCC)

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation - SCC (Strongly Connected Components)

  • SCC Algorithm is used to find the connected components in a graph.
  • Has two version
    • Kosaraju's algorithm
    • Tarjan's algorithm
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation - SCC (Strongly Connected Components)

  • Definition: a strongly connected component (SCCSCC) of a
    • directed graph G=(V,E)G=(V,E) is a maximal set of vertices UVU \subseteq V such that
    • For each u,vUu,v \in U we have both uvu \mapsto v and vuv \mapsto u
  • i.e., uu and vv are mutually reachable from each other (uvu \leftrightharpoons v)
  • Let GT=(V,ET)G^T=(V,E_T) be the transpose of G=(V,E)G=(V,E) where
    • ET={(u,v):(v,u)E}E^T=\{(u,v):(v,u) \in E\}
    • i.e., ETE^T consists of edges of GG with their directions reversed
    • Constructing GTG^T from GG takes O(V+E)O(V+E) time (adjacency list rep)
    • Note: GG and GTG^T have the same SCCSCCs (uvu \leftrightharpoons v in G    uvG \iff u \leftrightharpoons v in GTG^T)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation - SCC (Strongly Connected Components)

  • GT=(V,ET)G^T=(V,E^T) can create GTG^T \rightarrow Θ(V+E)\Theta(V+E) adjency list.
  • SCC(G)SCC(G) complexity is O(V+E)O(V+E)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation - SCC Algorithm

KOSARAJU-SCC(G)call DFS(G) compute all u.finishTime valuescompute GT=(V,ET) and reverse edge directionscall DFS(GT) but in the main loop,consider vertices in order of decreasing u.finishTime(as computed in DFS)output each DFTcomponent\begin{align*} & \text{KOSARAJU-SCC}(G) \\ & \quad call \ DFS(G) \ compute \ all \ u.finishTime \ values \\ & \quad compute \ G^T = (V,E^T) \ and \ reverse \ edge \ directions \\ & \quad call \ DFS(G^T) \ but \ in \ the \ main \ loop, \\ & \quad consider \ vertices \ in \ order \ of \ decreasing \ u.finishTime \\ & \qquad (as \ computed \ in \ DFS) \\ & \quad output \ each \ DFT component \end{align*}

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation - SCC - Kosaraju's algorithm

1- call DFS(G) compute all u.finishTime values
2- compute G^T = (V,E^T) and reverse edge directions
3- call DFS(G^T) but in the main loop, 
consider vertices 
in order of decreasing u.finishTime 
(as computed in DFS)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation - SCC - Kosaraju's algorithm

for each unvisited vertex u, DFS(u)
  try all free neighbor v of u, DFS(v)
  finish DFS(u), add u to the front of list
transpose the graph
DFS in order of the list, DFS(u)
  try all free neighbor v of u, DFS(v)
each time we complete a DFS, we get an SCC
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation - SCC - Tarjan's algorithm

for each unvisited vertex u
  DFS(u), s.push(u), num[u] = low[u] = DFSCount
    for each neighbor v of u
      if v is unvisited, DFS(v)
      low[u] = min(low[u], low[v])
    if low[u] == num[u] // root of an SCC
      pop from stack s until we get u
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation - SCC Algorithm - Example-1

Kosaraju's algorithm

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation - SCC Algorithm - Ex-1 / Step - 1

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Ex-1 / Step - 2

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1 / Step - 3

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 4

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 5

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 6

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 7

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 8

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 9

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 10

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 11

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 12

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 13

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 14

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 15

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 16

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 17

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 18

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 19

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 20

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 21

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 22

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 23

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 24

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 25

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 26

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 27

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 28

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 29

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 30

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 31

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 32

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 33

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 34

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 35

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 36

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 37

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 38

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 39

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 40

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 41

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

SCC Algorithm - Example-1/ Step - 42

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Segmentation

Strongly Connected Components Generate Acyclic Component Graph

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

BeginingofSCCProofBegining-of-SCC-Proof

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Strongly Connected Components

  • Lemma 1: no path between a pair of vertices in the same SCCSCC, ever leaves the SCCSCC
  • Proof: let uu and vv be in the same SCCuvSCC \Rightarrow u \leftrightharpoons v
  • let ww be on some path uwvuwu \mapsto w \mapsto v \Rightarrow u \mapsto w
  • but vuv \mapsto u \Rightarrow \exists a path wvuwuw \mapsto v \mapsto u \Rightarrow w \mapsto u
  • therefore uu and ww are in the same SCCSCC \Longrightarrow(Q.E.DQ.E.D)

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Strongly Connected Components

  • Theorem 1: in any DFSDFS, all vertices in the same SCCSCC are placed in the same DFTDFT
  • Proof: let rr be the first vertex discovered in SCCSCC SrS_r
    because rr is first, color[x]=WHITE xSrrcolor[x]=WHITE \ \forall x \in S_r-{r} at time d[r]d[r]
  • So all vertices are WHITEWHITE on each rxr \mapsto x path xSrr\forall x \in S_r-{r}
    • since these paths never leave Sr
  • Hence each vertex in SrrS_r-{r} becomes a descendent of rr (White-path Theorem) \Longrightarrow (Q.E.DQ.E.D)

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Notation for the Strongly Connected Components

  • d[u]d[u] and f[u]f[u] refer to those values computed by DFS(G)DFS(G) at step (1)
  • uvu \mapsto v refers to GG not GTG^T
  • Definition: forefather ϕ(u)\phi(u) of vertex uu
  1. ϕ(u)=\phi(u)= That vertex ww such that uwu \mapsto w and f[w]f[w] is maximized
  2. ϕ(u)=u\phi(u)=u possible because uuf[u]f[ϕ(u)]u \mapsto u \Rightarrow f[u] \leq f[\phi(u)]
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Strongly Connected Components

  • Lemma 2: ϕ(ϕ(u))=ϕ(u)\phi(\phi(u)) = \phi(u)

  • Proof try to show that f[ϕ(ϕ(u))]=f[ϕ(u)]:f[\phi(\phi(u))]=f[\phi(u)]:

    • For any u,vV;uvRvRuf[ϕ(v)]f[ϕ(u)]u,v \in V; u \mapsto v \Rightarrow R_v \subseteq R_u \Rightarrow f[\phi(v)] \leq f[\phi(u)]
    • So, uϕ(u)f[ϕ(ϕ(u))]f[ϕ(u)]u \mapsto \phi(u) \Rightarrow f[\phi(\phi(u))] \leq f[\phi(u)]
    • Due to definition of ϕ(u)\phi(u) we have f[ϕ(ϕ(u))]f[ϕ(u)]f[\phi(\phi(u))] \geq f[\phi(u)]
    • Therefore f[ϕ(ϕ(u))]=f[ϕ(u)]f[\phi(\phi(u))] = f[\phi(u)]
    • f[x]=f[y]x=yf[x]=f[y] \Rightarrow x = y (same vertex)

    center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Strongly Connected Components

  • Properties of forefather:
    • Every vertex in an SCCSCC has the same forefather which is in the SCCSCC
    • Forefather of an SCCSCC is the representative vertex of the SCCSCC
    • In the DFSDFS of GG, forefather of an SCCSCC is the
      • first vertex discovered in the SCCSCC
      • last vertex finished in the SCCSCC
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Strongly Connected Components

  • Theorem 2: ϕ(u)\phi(u) of any uVu \in V in any DFSDFS of GG is an ancestor of uu

  • Proof: Trivial if ϕ(u)=u\phi(u) = u.

  • If ϕ(u)u\phi(u) \neq u, consider color of ϕ(u)\phi(u) at time d[u]d[u]

    • ϕ(u)\phi(u) is GRAYGRAY: ϕ(u)\phi(u) is an ancestor of uu \Rightarrow proving the theorem
    • ϕ(u)\phi(u) is BLACKBLACK: f[ϕ(u)]<f[u]f[\phi(u)] < f[u] \Rightarrow contradiction to def. of ϕ(u)\phi(u)
    • ϕ(u)\phi(u) is WHITEWHITE: existexist 22 cases according to colors of intermediate vertices on p(u,ϕ(u))p(u, \phi(u))
  • Path p(u,ϕ(u))p(u, \phi(u)) at time d[u]d[u]:

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Strongly Connected Components

  • Case 1: every intermediate vertex xip(u,ϕ(u))x_i \in p(u, \phi(u)) is WHITEWHITE
    • ϕ(u)\Rightarrow \phi(u) becomes a descendant of uu (WhitePathTheoremWhite-Path-Theorem)
    • f[ϕ(u)]<f[u]\Rightarrow f [\phi(u)] < f [u]
    • \Rightarrow contradiction
  • Case 2: \exists some nonWHITEnon-WHITE intermediate vertices on p(u,ϕ(u))p(u, \phi(u))
    • Let xtx_t be the last nonWHITEnon-WHITE vertex on
      • p(u,ϕ(u))=u,x1,x2,,xr,ϕ(u)p(u, \phi(u)) = \langle u, x_1, x_2,\dots, x_r, \phi(u)\rangle
    • Then, xtx_t must be GRAYGRAY since BLACKtoWHITEBLACK-to-WHITE edge (xt,xt+1x_t, x_{t+1}) cannot exist
    • But then, p(xt,ϕ(u))=xt+1,xt+2,,xr,ϕ(u)p(x_t, \phi(u)) = \langle x_{t+1}, x_{t+2},\dots, x_r, \phi(u)\rangle is a white path
    • ϕ(u)\Rightarrow \phi(u) is a descendant of xtx_t (by white-path theorem)
    • f[xt]>f[ϕ(u)]f[x_t] > f[\phi(u)]
    • contradicting our choice for ϕ(u)\phi(u) \Longrightarrow Q.E.D.Q.E.D.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Strongly Connected Components

  • C1: in any DFSDFS of G=(V,E)G = (V, E) vertices uu and ϕ(u)\phi(u) lie in the same SCCSCC, uV\forall u \in V

  • Proof: uϕ(u)u \mapsto \phi(u) (by definition) and ϕ(u)u\phi(u) \mapsto u since ϕ(u)\phi(u) is an ancestor of uu (by Theorem 2)

  • Theorem 3: two vertices u,vVu,v \in V lie in the same SCC    ϕ(u)=ϕ(v)SCC \iff \phi(u) = \phi(v) in a DFSDFS of G=(V,E)G=(V,E)

  • Proof: let uu and vv be in the same SCCSCC CuvuvC_{uv} \Rightarrow u \leftrightharpoons v

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Strongly Connected Components

  • w:vwuw\forall w: v \mapsto w \Rightarrow u \mapsto w and w:uwvw\forall w: u \mapsto w \Rightarrow v \mapsto w, i.e.,
    • every vertex reachable from uu is reachable from vv and vice-versa
  • So, w=ϕ(u)w=ϕ(v)w = \phi(u) \Rightarrow w = \phi(v) and w=ϕ(v)w=ϕ(u)w = \phi(v) \Rightarrow w = \phi(u) by definition of forefather
  • Proof: Let ϕ(u)=ϕ(v)=wCwuCw\phi(u) = \phi(v) = w \in C_w \Rightarrow u \in C_w by C1C1 and vCwv \in C_w by C1C1
  • By Theorem 3: SCCSCCs are sets of vertices with the same forefather
  • By Theorem 2 and parenthesis Theorem: A forefather is the first vertex discovered and the last vertex finished in its SCCSCC

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

SCCSCC: Why do we Run DFSDFS on GTG^T?

  • Consider rVr \in V with largest finishing time computed by DFSDFS on GG
  • rr must be a forefather by definition since rrr \mapsto r and f[r]f[r] is maximum in VV
  • Cr=?:Cr=C_r = ?: Cr = vertices in rr’s SCC ={uV:ϕ(u)=r}= \{u \in V: \phi(u) = r\}
    • Cr={uV:ur and f[x]f[r]xRu}\Rightarrow C_r = \{u \in V: u \mapsto r \ and \ f[x] \leq f[r] \forall x \in R_u\}
      • where Ru={vV:uv}R_u =\{v \in V: u \mapsto v\}
    • Cr={uV:ur}\Rightarrow C_r = \{u \in V: u \mapsto r\} since f[r]f[r] is maximum
    • Cr=RrT={uV:ruGT}=\Rightarrow C_r = R_r^T = \{u \in V: r \mapsto u \in G^T\}= reachability set of rGTr \in G^T
  • i.e., Cr=C_r = those vertices reachable from rGTr \in G^T
  • Thus DFS-VISIT(GT,r)\text{DFS-VISIT}(G^T, r) identifies all vertices in CrC_r and
    • blackens them
RTEU CE100 Week-10
CE100 Algorithms and Programming II

SCCSCC: Why do we Run DFSDFS on GTG^T?

  • BFS(GT,r)BFS(G^T, r) can also be used to identify CrC_r
  • Then, DFSDFS on GTG^T continues with $DFS-VISIT(G^T, r')
    • where f[r]>f[w]wVCrf[r'] > f[w] \forall w \in V- C_r
  • rr must be a forefather by definition since rrr' \mapsto r' and
    • f[r]f[r'] is maximum in VCrV - C_r

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

SCCSCC: Why do we Run DFSDFS on GTG^T?

  • Hence by similar reasoning DFS-VISIT(GT,r)\text{DFS-VISIT}(G^T, r') identifies CrC_{r'}
  • Thus, each DFS-VISIT(GT,x)\text{DFS-VISIT}(G^T, x) in DFS(GT)DFS(G^T)
    • identifies an SCCSCC CxC_x with ϕ=x\phi = x

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

EndofSCCProofEnd-of-SCC-Proof

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Directed Acyclic Graphs (DAG)

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Directed Acyclic Graphs (DAG)

  • No Directed Cycles

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Directed Acyclic Graphs (DAG)

  • Theorem: a directed graph G is acyclic iff DFSDFS on GG yields no Back edges
  • Proof (acyclic  no Back edges; by contradiction):
  • Let (v,u) be a Back edge visited during scanning Adj[v]Adj[v]
    • color[v]=color[u]=GRAY\Rightarrow color[v] = color[u] = GRAY and d[u]<d[v]d[u] < d[v]
    • int[v]\Rightarrow int[v] is contained in int[u]vint[u] \Rightarrow v is descendent of uu
    • \Rightarrow \exists a path from uu to vv in a DFTDFT and hence in GG
    • \therefore edge (v,u)(v,u) will create a cycle (Back edge \Rightarrow cycle)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Directed Acyclic Graphs (DAG) - aAcyclic iff no Back edges

  • Proof (no Back edges \Rightarrow acyclic):
    • Suppose GG contains a cycle CC (Show that a DFSDFS on GG yields a BackEdgeBack Edge; proof by contradiction)
    • Let vv be the first vertex discovered in CC and let (u,v)(u,v) be proceeding edge in CC
    • At time d[v]:d[v]: \exists a white path from vv to uu along CC
    • By WhitePathWhite Path Thrm uu becomes a descendent of vv in a DFTDFT
    • Therefore (u,v)(u,v) is a BackEdgeBack Edge (descendent to ancestor)

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Topological Sort of a DAG

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal - Topological Sort of a DAG

  • When we are scheduling jobs or tasks, they may have dependencies.

  • For example, before we finish task a, we have to finish b first.

    • In this case, given a set of tasks and their dependencies, how shall we arrange our schedules? There comes an interesting graph algorithm: Topological Sort.
  • According to Introduction to Algorithms, given a directed acyclic graph (DAG),

  • a topological sort is a linear ordering of all vertices such that for any edge (u, v), u comes before v.

  • Another way to describe it is that when you put all vertices horizontally on a line, all of the edges are pointing from left to right.

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal - Topological Sort of a DAG

  • Topological sort is a linear ordering of a directed acyclic graph.

  • If a graph has a cycle, it is not a directed acyclic graph.

  • A graph is acyclic if it has no cycles.

  • Linear ordering "<<" of VV such that

    • (u,v)Eu<v(u,v) \in E \Rightarrow u < v in ordering
      • Ordering may not be unique
      • i.e., mapping the partial ordering to total ordering may yield more than one orderings
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal -Topological Sort of a DAG

DFS version

  • The key observation is that, leaf nodes should always come after their parents and ancestors. Following this intuition we can apply DFS and output nodes from leaves to the root.

  • We need to implement a boolean array visited so that visited[i] indicates if we have visited vertex i.

  • For each unvisited node, we would first mark it as visited and call DFS() to start searching its neighbours.

  • After finishing this, we can insert it to the front of a list. After visiting all nodes, we can return that list.

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort of a DAG

DFS version

run DFS(G)
when a vertex finished, output it
vertices output in **reverse** topologically sorted order
  • Runs in O(V+E) time
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort of a DAG

DFS version

def topological_sort():
    for each node:
        if visited[node] is False:
            dfs(node)

def dfs(node):
    visited[node] = True
    for nei in neighbours[node]:
        dfs(node)
	if visited(node) = false:
		ret.insert_at_the_front(node)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort of a DAG

DFS version

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - DFS Version STEP-1

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - DFS Version STEP-2

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - DFS Version STEP-3

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - DFS Version STEP-4

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - DFS Version STEP-5

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - DFS Version STEP-6

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - DFS Version STEP-7

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - DFS Version STEP-8

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - DFS Version STEP-9

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - DFS Version STEP-10

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - DFS Version

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort of a DAG

BFS version (Kahn's algorithm)

  • For BFS, we need an array indegree to keep the track of indegrees. Then we will try to output all nodes with 0 indegree, and remove the edges coming out of them at the same time. Besides, remember to put the nodes that become 0 indegree in the queue.

  • Then, we can keep doing this until all nodes are visited. To implement it, we can store the graph in an adjacent list (a hashmap or a dictionary in Python) and a queue to loop.

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort of a DAG

BFS version (Kahn's algorithm)

indegree = an array indicating indegrees for each node
neighbours = a HashMap recording neighbours of each node
queue = []
for i in indegree:
    if indegree[i] == 0:
        queue.append(i)
		
while !queue.empty():
    node = queue.dequeue()
    for neighbour in neighbours[node]:
        indegree[neighbour] -= 1
        if indegree[neighbour] == 0:
            queue.append(neighbour)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - BFS version (Kahn's algorithm)

  • STEP-1

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - BFS version (Kahn's algorithm)

  • STEP-2

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - BFS version (Kahn's algorithm)

  • STEP-3

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - BFS version (Kahn's algorithm)

  • STEP-4

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - BFS version (Kahn's algorithm)

  • STEP-5

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - BFS version (Kahn's algorithm)

  • STEP-6

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - BFS version (Kahn's algorithm)

  • STEP-7

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - BFS version (Kahn's algorithm)

  • STEP-8

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - BFS version (Kahn's algorithm)

  • STEP-9

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - BFS version (Kahn's algorithm)

  • STEP-10

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort - BFS version (Kahn's algorithm)

  • STEP-11 (Final)

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Traversal

Topological Sort of a DAG

Correctness of the Algorithm

  • Claim: (u,v)Ef[u]>f[v](u,v) \in E \Rightarrow f[u] > f[v]
  • Proof: consider any edge (u,v)(u,v) explored by DFSDFS
  • when (u,v)(u,v) is explored, uu is GRAYGRAY
    • if vv is GRAYGRAY, (u,v)(u,v) is a Back edge (contradicting acyclic theorem)
    • if vv is WHITEWHITE, vv becomes a descendent of uu (b WPT) f[v]<f[u]\Rightarrow f[v] < f[u]
    • if vv is BLACKBLACK, f[v]<d[u]f[v]<f[u]f[v] < d[u] \Rightarrow f[v] < f[u]
      Q.E.DQ.E.D
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Topological Sort of a DAG - Getting Dressed Example

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Cycle Detection

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Detect Cycle in a Directed Graph

Approach:

  • Depth First Traversal can be used to detect a cycle in a Graph.
  • DFS for a connected graph produces a tree.
  • There is a cycle in a graph only if there is a back edge present in the graph.
  • A back edge is an edge that is
    • from a node to itself (self-loop) or
    • one of its ancestors in the tree produced by DFS.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Detect Cycle in a Directed Graph

Algorithm:

  • Create the graph using the given number of edges and vertices.
  • Create a recursive function that initializes the current index or vertex, visited, and recursion stack.
  • Mark the current node as visited and also mark the index in recursion stack.
  • Find all the vertices which are not visited and are adjacent to the current node. Recursively call the function for those vertices, If the recursive function returns true, return true.
  • If the adjacent vertices are already marked in the recursion stack then return true.
  • Create a wrapper class, that calls the recursive function for all the vertices and if any function returns true return true. Else if for all vertices the function returns false return false.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Detect Cycle in a Directed Graph

  • Complexity Analysis:
    • Time Complexity: O(V+E)O(V+E).
      • Time Complexity of this method is same as time complexity of DFSDFS traversal which is O(V+E)O(V+E).
  • Space Complexity: O(V)O(V).
    • To store the visited and recursion stack O(V)O(V) space is needed.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Detect cycle in an undirected graph

Approach:

  • Run a DFS from every unvisited node.
  • Depth First Traversal can be used to detect a cycle in a Graph.
  • DFS for a connected graph produces a tree.
  • There is a cycle in a graph only if there is a back edge present in the graph.
  • A back edge is an edge that is joining a node to
    • itself (self-loop) or
    • one of its ancestor in the tree produced by DFS.
  • To find the back edge to any of its ancestors
    • keep a visited array and if there is a back edge to any visited node
    • then there is a loop and return true.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Detect cycle in an undirected graph

Algorithm:

  • Create the graph using the given number of edges and vertices.
  • Create a recursive function that have current index or vertex, visited array and parent node.
  • Mark the current node as visited .
  • Find all the vertices which are not visited and are adjacent to the current node.
    • Recursively call the function for those vertices, If the recursive function returns true return true.
  • If the adjacent node is not parent and already visited then return true.
  • Create a wrapper class, that calls the recursive function for all the vertices and if any function returns true, return true.
  • Else if for all vertices the function returns false return false.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Detect cycle in an undirected graph

Complexity Analysis:

  • Time Complexity: O(V+E)O(V+E).
    • The program does a simple DFS Traversal of the graph which is represented using adjacency list. So the time complexity is O(V+E)O(V+E).
  • Space Complexity: O(V)O(V).
    • To store the visited array O(V)O(V) space is required.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Coloring

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Coloring

  • Given an undirected graph and a number m,
  • determine if the graph can be coloured with at most m colours such that no two adjacent vertices of the graph are colored with the same color.
  • Here coloring of a graph means the assignment of colors to all vertices.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Coloring

Naive Approach:

  • Generate all possible configurations of colors.
  • Since each node can be coloured using any of the m available colours,
  • the total number of colour configurations possible are mVm^V.
  • After generating a configuration of colour,
    • check if the adjacent vertices have the
      • same colour or not.
    • If the conditions are met,
    • print the combination and break the loop.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Coloring

Naive Algorithm:

  • Create a recursive function that takes current index, number of vertices and output color array.
  • If the current index is equal to number of vertices.
    • Check if the output color configuration is safe,
      • i.e check if the adjacent vertices do not have same color.
    • If the conditions are met,
      • print the configuration and break.
  • Assign a color to a vertex (1 to m).
  • For every assigned color
    • recursively call the function with next index and number of vertices
  • If any recursive function returns true break the loop and returns true.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Coloring

Naive Complexity Analysis:

  • Time Complexity: O(mV)O(m^V).
    • There is a total O(mV)O(m^V) combination of colors. So the time complexity is O(mV)O(m^V).
  • Space Complexity: O(V)O(V).
    • Recursive Stack of graphColoring()graphColoring(\dots) function will require O(V)O(V) space.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Coloring

Backtracking Approach:

  • The idea is to assign colors one by one to different vertices,
    • starting from the vertex 0.
  • Before assigning a color, check for safety by considering already assigned colors to the adjacent vertices
    • i.e check if the adjacent vertices have the same color or not.
  • If there is any color assignment that does not violate the conditions,
  • mark the color assignment as part of the solution.
  • If no assignment of color is possible then backtrack and return false.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Coloring

Backtracking Algorithm:

  • Create a recursive function that takes the graph, current index, number of vertices, and output color array.
  • If the current index is equal to the number of vertices. Print the color configuration in output array.
  • Assign a color to a vertex (1 to m).
  • For every assigned color,
    • check if the configuration is safe,
      • (i.e. check if the adjacent vertices do not have the same color)
    • recursively call the function with next index and number of vertices
  • If any recursive function returns true break the loop and return true.
  • If no recursive function returns true then return false.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Coloring

Using BFS Approach / Algorithm

  • The approach here is to color each node from 1 to n
  • initially by color 1.
  • And start travelling BFS from an unvisited starting node to cover all connected components in one go.
  • On reaching each node during BFS traversal, do the following:
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Coloring

Using BFS Approach / Algorithm

  • Check all edges of the given node.
  • For each vertex connected to our node via an edge:
    • check if the color of the nodes is the same.
      • If same,
        • increase the color of the other node (not the current) by one.
    • check if it visited or unvisited.
      • If not visited,
        • mark it as visited and push it in a queue.
  • Check condition for maxColors till now.
    • If it exceeds M, return false
  • After visiting all nodes,
    • return true (As no violating condition could be found while travelling).
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Graph Coloring

Using BFS Complexity Analysis:

  • Time Complexity: O(V+E)O(V + E).
  • Space Complexity: O(V)O(V).
    • For Storing Visited List.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Biparitite Checker

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Biparitite Checker

  • A Bipartite Graph is a graph
    • whose vertices can be divided into
      • two independent sets,
      • U and V such that every edge (u, v) either connects a vertex from U to V or a vertex from V to U.
    • In other words, for every edge (u, v),
    • either u belongs to U and v to V,
    • or u belongs to V and v to U.
  • We can also say that there is no edge that connects vertices of same set.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Biparitite Checker

  • A bipartite graph is possible if the graph coloring is possible using two colors such that vertices in a set are colored with the same color.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Biparitite Checker

center

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Biparitite Checker

  • Note that it is possible to color a cycle graph with even cycle using two colors.

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Biparitite Checker

  • It is not possible to color a cycle graph with odd cycle using two colors.

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Biparitite Checker Algorithm

  • One approach is to check whether the graph is 2-colorable or not using backtracking algorithm m coloring problem.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Biparitite Checker Algorithm

  • Following is a simple algorithm to find out whether a given graph is Bipartite or not using
    • Breadth First Search (BFS).
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Biparitite Checker Algorithm

  1. Assign RED color to the source vertex (putting into set U).
  2. Color all the neighbors with BLUE color (putting into set V).
  3. Color all neighbor’s neighbor with RED color (putting into set U).
  4. This way, assign color to all vertices such that it satisfies all the constraints of m way coloring problem where m = 2.
  5. While assigning colors, if we find a neighbor which is colored with same color as current vertex, then the graph cannot be colored with 2 vertices (or graph is not Bipartite)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Disjoint Set Operations

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Disjoint Set Operations

  • A disjoint-set data structure
    • Maintains a collection S={s1,,sk}S=\{s_1,\dots,s_k\} of disjoint dynamic sets
    • Each set is identified by a representative which is some member of the set
  • In some applications,
    • It doesn't matter which member is used as the representative
    • We only care that,
      • if we ask for the representative of a set twice without
        • modifying the set between the requests,
      • we get the same answer both times
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Disjoint Set Operations

  • In other applications,
  • There may be a prescribed rule for choosing the representative
    • E.G. Choosing the smallest member in the set
  • Each element of a set is represented by an object "xx"
  • MAKE-SET(x)\text{MAKE-SET}(x) creates a new set whose only member is xx
    • Object xx is the representative of the set
    • xx is not already a member of any other set
  • UNION(x,y)\text{UNION}(x, y) unites the dynamic sets Sx&SyS_x\&S_y that contain x&yx\&y
    • Sx&SyS_x\&S_y are assumed to be disjoint prior to the operation
    • The new representative is some member of SxSyS_x \cup S_y
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Disjoint Set Operations

  • Usually, the representative of either SxS_x or SyS_y is chosen as the new representative
  • We destroy sets SxS_x and SyS_y, removing them from the collection SS since we require the sets in the collection to be disjoint
  • FIND-SET(x)\text{FIND-SET}(x) returns a pointer to the representative of the unique set containing x
  • We will analyze the running times in terms of two parameters
    • nn : The number of MAKE-SET\text{MAKE-SET} operations
    • mm : The total number of MAKE-SET\text{MAKE-SET}, UNION\text{UNION} and FIND-SET\text{FIND-SET} operations
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Disjoint Set Operations

  • Each union operation reduces the number of sets by one
    • since the sets are disjoint
    • Therefore, only one set remains after n1n - 1 union operations
    • Thus, the number of union operations is $ \leq n – 1$
  • Also note that, mnm \geq n always hold
    • since MAKE-SET\text{MAKE-SET} operations are included in the total number of operations
RTEU CE100 Week-10
CE100 Algorithms and Programming II

An Application of Disjoint-Set Data Structures

  • Determining the connected components of an undirected graph G=(V,E)G=(V,E)

CONNECTED-COMPONENTS(G)for each vertex vV[G] doMAKE-SET(v)endforfor each edge (u,v)E[G] doif FIND-SET(u)FIND-SET(v) thenUNION(u,v)endifendforend\begin{align*} & \text{CONNECTED-COMPONENTS}(G)\\ & \quad for \ each \ vertex \ v \in V[G] \ do \\ & \qquad \text{MAKE-SET}(v) \\ & \quad endfor \\[8pt] & \quad for \ each \ edge \ (u, v) \in E[G] \ do \\[5pt] & \qquad if \ \text{FIND-SET}(u) \neq \text{FIND-SET}(v) \ then \\ & \qquad \quad UNION(u, v) \\ & \qquad endif \\[5pt] & \quad endfor \\ &end \end{align*}

RTEU CE100 Week-10
CE100 Algorithms and Programming II

An Application of Disjoint-Set Data Structures

SAME-COMPONENT(u,v)if FIND-SET(u)=FIND-SET(v) thenreturn TRUEelsereturn FALSEendifend\begin{align*} & \text{SAME-COMPONENT}(u,v)\\ & \quad if \ \text{FIND-SET}(u) = \text{FIND-SET}(v) \ then \\ & \qquad return \ TRUE \\ & \quad else \\ & \qquad return \ FALSE \\ & \quad endif \\ & end \end{align*}

RTEU CE100 Week-10
CE100 Algorithms and Programming II

An Application of Disjoint-Set Data Structures

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Linked-List Representation of Disjoint Sets

  • Represent each set by a linked-list
  • The first object in the linked-list serves as its set representative
  • Each object in the linked-list contains
    • A set member
    • A pointer to the object containing the next set member
    • A pointer back to the representative
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Linked-List Representation of Disjoint Sets

  • MAKE-SET(x)\text{MAKE-SET}(x) : O(1)O(1)

center

  • FIND-SET(x)\text{FIND-SET}(x) : We return the representative pointer of xx
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Linked-List Representation of Disjoint Sets

  • A Simple Implementation of Union : UNION(x,y)UNION(x, y)
    • APPEND\text{APPEND} xx's list to the end of yy's list
      • The representative of yy's list becomes the new representative
    • UPDATE\text{UPDATE} the representative pointer of each object originally on xx's list which takes time linear in the length of xx's list
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Linked-List Representation of Disjoint Sets

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Analysis of the Simple Union Implementation

OperationNumberofObjectstUpdatedUpdatedObjectsMAKE-SET(X1)1{x1}MAKE-SET(X2)1{x2}MAKE-SET(Xn)1{xn}UNION(X1,X2)1{x1}{x2}{x1,x2}UNION(X2,X3)2{x1,x2}{x3}{x1,x2,x3}UNION(X3,X4)3{x1,x2,x3}{x4}{x1,x2,x3,x4}UNION(Xn1,Xn)n1{x1,x2,,xn1}{xn}{x1,x2,x3,,xn1,xn}\begin{matrix} Operation & Number of Objectst Updated & Updated Objects^*\\ --- & --- & ---\\ \text{MAKE-SET}(X_1) & 1 & \{x_1^*\} \\ \text{MAKE-SET}(X_2) & 1 & \{x_2^*\} \\ \vdots & \vdots & \vdots \\ \text{MAKE-SET}(X_n) & 1 & \{x_n^*\} \\ \text{UNION}(X_1,X_2) & 1 & \{x_1\} \cup \{x_2\} \leftarrow \{x_1^*,x_2\} \\ \text{UNION}(X_2,X_3) & 2 & \{x_1,x_2\} \cup \{x_3\} \leftarrow \{x_1^*,x_2^*,x_3\} \\ \text{UNION}(X_3,X_4) & 3 & \{x_1,x_2,x_3\} \cup \{x_4\} \leftarrow \{x_1^*,x_2^*,x_3^*,x_4\} \\ \vdots & \vdots & \vdots \\ \text{UNION}(X_{n-1},X_n) & n-1 & \{x_1,x_2,\dots,x_{n-1}\} \cup \{x_n\} \leftarrow \{x_1^*,x_2^*,x_3^*,\dots,x^*_{n-1},x_n\} \\ \end{matrix}

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Analysis of the Simple Union Implementation

  • The total number of representative pointer updates
    • nMAKESET+i=1n1iUNION=n+12(n1)n=12n2+12n=Θ(n2)\overbrace{n}^{MAKE-SET}+ \overbrace{\sum\limits_{i=1}^{n-1}i}^{UNION} = n + \frac{1}{2}(n-1)n = \frac{1}{2}n^2 + \frac{1}{2}n = \Theta(n^2)
    • =Θ(m2)=\Theta(m^2) since n=m/2n=\lceil m/2 \rceil
  • Thus, on the average, each operation requires Θ(m)\Theta(m) time
  • That is, the amortized time of an operation is Θ(m)\Theta(m)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

A Weighted-Union Heuristic

  • The simple implementation is inefficient because

    • We may be appending a longer list to a shorter list during a UNION\text{UNION} operation
      • so that we must update the representative pointer of each member of the longer list
  • Maintain the length of each list

  • Always append the smaller list to the longer list

    • With ties broken arbitrarily
  • A single UNION\text{UNION} can still take Ω(m)\Omega(m) time if both sets have Ω(m)\Omega(m) members

RTEU CE100 Week-10
CE100 Algorithms and Programming II

A Weighted-Union Heuristic

  • Theorem: A sequence of mm MAKE-SET,UNION&FIND-SET\text{MAKE-SET}, \text{UNION} \& \text{FIND-SET} operations, nn of which are MAKE-SET\text{MAKE-SET} operations, takes O(m+nlgn)O(m+nlgn) time
  • Proof: Try to compute an upper bound on the number of representative pointer updates for each object in a set of size nn
  • Consider a fixed object xx
  • Each time xx’s RPTRR-PTR was updated, xx was a member of the smaller set
  • {x}{v}{x,v} 1-st update Sx2\{x\} \cup \{v\} \rightarrow \{x^*,v\} \Longrightarrow \ \text{1-st update} \ |S_x| \geq 2
  • {x,v}{w1,w2}{x,v,w1,w2} 2-nd update Sx4\{x, v\} \cup \{w_1, w_2\} \rightarrow \{ x^*,v^*,w_1,w_2\} \Longrightarrow \ \text{2-nd update} \ |S_x| \geq 4
  • {x,v,w1,w2}{z1,z2,z3,z4}{x,v,,w1,w2,z1,z2,z3,z4};Sx4\{x,v,w_1,w_2\} \cup \{z_1,z_2,z_3,z_4\} \rightarrow \{x^*,v^*,,w_1^*,w_2^*,z_1,z_2,z_3,z_4\}; |S_x| \geq 4
  • 3-rd update S8\text{3-rd update} \ |S| \geq 8
RTEU CE100 Week-10
CE100 Algorithms and Programming II

A Weighted-Union Heuristic

  • For any knk \leq n, after xx’s RPTRR-PTR has been updated lgk\lceil lg k \rceil times the resulting set must have at least kk members
  • RPTRR-PTR of each object can be updated at most lgk\lceil lg k \rceil time over all UNION\text{UNION} operations
  • Analysis of The Weighted-Union Heuristic
    • The below illustrates a worst case sequence for a set with n=16n = 16 objects
    • The total number of RPTRR-PTR updates

=162×1+164×2+168×4+1616×8=8×1+4×2+2×4+1×8=8×4=32=n2+n2++n2lgn=n2lgn=O(nlgn)\begin{align*} &= \frac{16}{2}\times 1+\frac{16}{4}\times 2+\frac{16}{8}\times 4+\frac{16}{16}\times 8 \\ &= 8 \times 1 +4 \times 2 +2 \times 4 +1 \times 8 \\ &= 8 \times 4 \\ &= 32 \\ &= \underbrace{\frac{n}{2}+\frac{n}{2}+\dots+\frac{n}{2}}_{lgn} = \frac{n}{2}lgn = O(nlgn) \end{align*}

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Analysis of The Weighted-Union Heuristic

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Analysis of The Weighted-Union Heuristic

  • Each MAKE-SET&FIND-SET\text{MAKE-SET} \& \text{FIND-SET} operation takes O(1)O(1) time, and there are O(m)O(m) of them
  • The total time for the entire sequence =O(m+nlgn)=O(m+nlgn)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Disjoint Set Forests

  • In a faster implementation, we represent sets by rooted trees
    • Each node contains one member
    • Each tree represents one set
    • Each member points only to its parent
    • The root of each tree contains the representative
    • Each root is its own parent
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Disjoint Set Forests

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Disjoint Set Forests - Straightforward Implementation

  • MAKE-SET\text{MAKE-SET} : Simply creates a tree with just one node : O(1)O(1)
  • FIND-SET\text{FIND-SET} : Follows parent pointers until the root node is found
    • The nodes visited on this path toward the root constitute the FIND-PATH\text{FIND-PATH}
  • UNION\text{UNION}: Makes the root of one tree to point to the other one
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Disjoint Set Forests - Heuristics To Improve the Running Time

  • Straightforward implementation is no faster than ones that use the linked-list representation
  • A sequence of n1n – 1 UNION\text{UNION}'s, following a sequence of n MAKE-SET\text{MAKE-SET}'s, may create a tree, which is just a linear chain of nn nodes
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Disjoint Set Forests - Heuristics To Improve the Running Time

First Heuristic : UNION by Rank

  • Similar to the weighted-union used for the linked-list representation
  • The idea is to make the root of the tree with fewer nodes point to the root of the tree with more nodes
  • Rather than explicitly keeping the size of the subtree
    • rooted at each node
      • We maintain a rank
      • that approximates the logarithm of the subtree size
      • and is also an upperbound on the height of the node
  • During a UNION\text{UNION} operation
    • make the root with smaller rank to point to the root with larger rank
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Disjoint Set Forests - Heuristics To Improve the Running Time

Second Heuristic : Path Compression

  • Use it during the FIND-SET\text{FIND-SET} operations
    • Make each node on the FIND-PATH\text{FIND-PATH} to point directly to the root

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Disjoint Set Forests - Heuristics To Improve the Running Time

Path Compression During FIND-SET(b) Operation

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Disjoint Set Forests - Pseudocodes For the Heuristics

Implementation of UNION-BY-RANK Heuristic

  • p[x]p[x]: Pointer to the parent of the node xx
  • rank[x]rank[x]: An upperbound on the height of node xx in the tree

MAKE-SET(x)p[x]xrank[x]0end\begin{align*} & \text{MAKE-SET}(x) \\ & \quad p[x] \rightarrow x \\ & \quad rank[x] \rightarrow 0 & end \end{align*}

\dots

UNION(x,y)LINK(FIND-SET(x),FIND-SET(y))end\begin{align*} & \text{UNION}(x,y) \\ & \quad \text{LINK}(\text{FIND-SET}(x),\text{FIND-SET}(y)) \\ & end \end{align*}

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Disjoint Set Forests - Pseudocodes For the Heuristics

Implementation of UNION-BY-RANK Heuristic

LINK(x,y)if rank[x]>rank[y] thenp[y]xelsep[x]yif rank[x]=rank[y] thenrank[y]=rank[y]+1endifendifend\begin{align*} & \text{LINK}(x,y) \\ & \quad if \ rank[x] > rank[y] \ then \\ & \qquad p[y] \rightarrow x \\ & \quad else \\ & \qquad p[x] \rightarrow y \\ & \qquad if \ rank[x] = rank[y] \ then \\ & \qquad \quad rank[y] = rank[y] + 1 \\ & \qquad endif \\ & \quad endif \\ & end \end{align*}

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Implementation of UNION-BY-RANK Heuristic

  • When a singleton set is created by a MAKE-SET\text{MAKE-SET}
    • the initial rank of the single node in the tree is zero
  • Each FIND-SET\text{FIND-SET} operation leaves all ranks unchanged
  • When applying a UNION\text{UNION} to two trees,
    • we make the root of tree with higher rank
      • the parent of the root of lower rank
  • **Ties are broken arbitrarily **
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Implementation of the Path-Compression Heuristic

The FIND-SET procedure with Path-Compression

  • Iterative Version

FIND-SET(x)yxwhile yp[y] doyp[y]endwhilerootywhile xp[x] doparentp[x]p[x]rootxparentendwhilereturn rootend\begin{align*} & \text{FIND-SET}(x) \\ & \quad y \leftarrow x \\ & \quad while \ y \neq p[y] \ do \\ & \qquad y \leftarrow p[y] \\ & \quad endwhile \\ & \quad root \leftarrow y \\ & \quad while \ x \neq p[x] \ do \\ & \qquad parent \leftarrow p[x] \\ & \qquad p[x] \leftarrow root \\ & \qquad x \leftarrow parent \\ & \quad endwhile \\ & \quad return \ root \\ & end \end{align*}

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Implementation of the Path-Compression Heuristic

The FIND-SET procedure with Path-Compression

  • Recursive Version

FIND-SET(x)if xp[x] thenp[x]FIND-SET(p[x])endifreturn p[x]end\begin{align*} & \text{FIND-SET}(x) \\ & \quad if \ x \neq p[x] \ then \\ & \qquad p[x] \leftarrow \text{FIND-SET}(p[x]) \\ & \quad endif \\ & \quad return \ p[x] \\ & end \end{align*}

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Analysis Of Union By Rank With Path Compression

  • When we use both union-by-rank and path-compression the worst case running time is O(mα(m,n))O(m\alpha(m,n)) where α(m,n)\alpha(m,n) is the very slowly growing inverse of the Ackerman’s function.

-In any conceivable application of disjoint-set data structure α(m,n)4\alpha(m,n) \leq 4.

  • Thus, we can view the running time as linear in practical situations.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Minimum Spanning Tree (MST)

  • Kruskal
  • Prim
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Minimum Spanning Tree

  • One of the most famous greedy algorithms.
  • Weight is minimum over all
  • It has V1|V|-1 edges
  • It has no cycles
  • It might not be unique
  • Undirected Graph G=(V,E)G =(V,E)
  • Connected
  • Weight Function ω:ER\omega : E \rightarrow R
  • Spanning Tree : Tree that connects all vertices
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Minimum Spanning Tree

  • MST : ω(T)=(u,v)Tω(u,v)\omega(T) = \sum(u,v) \in T \omega(u,v)
  • Note : MST is not unique.

center

RTEU CE100 Week-10
CE100 Algorithms and Programming II

MST-Optimal Structure

  • Optimal Structure: Optimal tree has optimal subtrees.
    • Let TT be an MSTMST of G=(V,E)G=(V,E)
    • Removing any edge (u,v)(u,v) of TT partitions TT into two subtrees : T1&T2T_1 \& T_2
    • Where T1=(V1,ET1)&T2=(V2,ET2)T_1 = (V_1,E_{T_1}) \& T_2 = (V_2, E_{T_2})
RTEU CE100 Week-10
CE100 Algorithms and Programming II

MST-Optimal Structure

  • Let G1=(V1,E1)&G2=(V2,E2)G_1=(V_1,E_1) \& G_2=(V_2,E_2) be
    • subgraphs induced by V1&V2V_1 \& V_2
      • i.e. Ei={(x,y)E:x,yVi}E_i=\{(x,y) \in E : x, y \in V_i\}
    • Claim : T1&T2T_1 \& T_2 are MSTMSTs of G1&G2G_1 \& G_2 respectively
    • Proof : ω(T)=ω(u,v)+ω(T1)+ω(T2)\omega(T) = \omega(u,v) + \omega(T1) + \omega(T2)
  • There can’t be better trees than T1&T2T_1\&T_2 for G1&G2G_1\&G_2
  • Otherwise, TT would be suboptimal for GG
RTEU CE100 Week-10
CE100 Algorithms and Programming II

Generic MST Algorithm

  • AA is always a subset of some MST(s)MST(s)
  • (u,v)(u,v) is a safe edge for AA if A{(u,v)}A \cup \{ (u,v) \} is also a subtree of some MSTMST

GENERIC-MST(G,ω)Awhile A does not form a spanning tree dofind a safe edge (u,v) for AAA{(u,v)}return Aend\begin{align*} & \text{GENERIC-MST}(G, \omega) \\ & \quad A \leftarrow \empty \\ & \quad while \ A \ does \ not \ form \ a \ spanning \ tree \ do \\ & \qquad find \ a \ safe \ edge \ (u,v) \ for \ A \\ & \qquad A \leftarrow A \cup \{ (u,v) \} \\ & \quad return \ A \\ & end \end{align*}

RTEU CE100 Week-10
CE100 Algorithms and Programming II

Generic MST Algorithm

  • One safe edge must exist at each step since :
  • ATA \subset T where TT is an MSTMST
  • Let (u,v)T(u,v)A(u,v)(u,v) \in T (u,v) \backepsilon A \Rightarrow (u,v) is safe for AA
  • A cut (S,VS)(S,V-S) of G=(V,E)G=(V,E) is a Partition of VV
  • An edge (u,v)E(u,v) \in E crosses the cut (S,VS)(S,V-S)
    • if uS&vVSu \in S \& v \in V-S or vice versa
  • A cut respects the set AA of edges if no edge in AA crosses the cut
  • An edge is a light edge crossing a cut
    • If its weight is the minimum of any edges crossing the cut
    • There can be more than one light edge crossing the cut in the case of ties.
RTEU CE100 Week-10
CE100 Algorithms and Programming II

TODO - Missing Parts...

RTEU CE100 Week-10
CE100 Algorithms and Programming II

MST - Kruskal Algorithm

  • Sort the graph edges with weight
  • Add from minimum weights
  • Only add edges which doesn't form a cycle
  • Disjoint Sets
    • MAKE-SET
    • FIND-SET
    • UNION
RTEU CE100 Week-10
CE100 Algorithms and Programming II

MST - Kruskal Algorithm

MST-KRUSKAL(G,ω)Afor each vertex vV[G] doMAKE-SET(v)SORT the edges of E by nondecreasing weight ωfor each edge (u,v)E in nondecreasing order doifFIND-SET(u)FINDSET(v) thenAA{(u,v)}UNION(u,v)returnAend\begin{align*} & \text{MST-KRUSKAL}(G, \omega) \\ & \quad A \leftarrow \empty \\ & \quad for \ each \ vertex \ v \in V[G] \ do \\ & \qquad \text{MAKE-SET}(v) \\ & \quad SORT \ the \ edges \ of \ E \ by \ nondecreasing \ weight \ \omega \\ & \quad for \ each \ edge \ (u,v) \in E \ in \ nondecreasing \ order \ do \\ & \qquad if \text{FIND-SET}(u) \neq FIND-SET(v) \ then \\ & \qquad \quad A \leftarrow A \cup \{(u,v)\} \\ & \qquad \quad UNION (u,v) \\ & \quad return A \\ & end \end{align*}

RTEU CE100 Week-10
CE100 Algorithms and Programming II

MST - Kruskal Analysis of Algorithm

  • Depends on Implementation of Disjoint Sets
  • init set take O(1)O(1)
  • sort edge O(ElogE)O(ElogE)
  • for loop FIND-SET and UNION O(E)O(E)
  • V|V|
  • O((V+E)α(V))O((V+E)\alpha(V))
  • EV1|E|\leq |V|-1
    • logE=O(logV)logE = O(logV)
    • O(ElgV)O(ElgV)
  • Total Time = O(ElgE)O(ElgE)
RTEU CE100 Week-10
CE100 Algorithms and Programming II

MST - Prim

RTEU CE100 Week-10
CE100 Algorithms and Programming II

TBDTBD

RTEU CE100 Week-10
CE100 Algorithms and Programming II

References

RTEU CE100 Week-10
CE100 Algorithms and Programming II

EndOfWeek10CourseNotesEnd-Of-Week-10-Course-Notes

RTEU CE100 Week-10

--- ## **Graph Traversal** ### Topological Sort #### DFS version ```cpp for each unvisited vertex u DFS(u) for each neighbor v of u if v is unvisited, DFS(v) else skip v; finish DFS(u), add u to the back of list reverse list ```