You are on page 1of 11

Week-11

Mathematics for Data Science - 1


Directed Acylcic Graphs, Longest Paths, Transitive Closure, Shortest Path,
Single Source Shortest Paths, All Pair Shortest Path, Minimum Cost
Spanning Tree, Prim’s and Kruskal’s Algorithm
Solve with us Assignment

1. Key Points: Directed Acyclic Graph

• G=(V,E), a graph without directed cycle


• DAG are natural way to represent dependencies.
• Topological Sorting: Enumerate V = {0,1,..., n − 1} such that for any (i, j) ∈ E,
i appears before j
• A graph with Directed Cycles cannot be Topologically sorted.
• Every DAG can be topologically sorted.
• A vertex with no dependencies has no incoming edges (in-degree(v)=0)
• Every DAG has a vertex with in-degree 0
• We can topologically sort a DAG in more than one ways
(a) Which of the following Option is(are) correct?
⃝ Option 1: A graph with directed cycle can be topologically sorted
⃝ Option 2: A vertex with zero in-degree has two incoming edges
⃝ Option 3:A vertex with zero in-degree has no incoming edges
⃝ Option 4: Only one Topological is possible for any Directed Acyclic Graph
⃝ Option 5: Every DAG has a vertex with in-degree 0

2. Key points: Longest Paths in DAG

• Compute indegree of each vertex


• Initialize longest-path-to to 0 for all vertices
• List a vertex with indegree 0 and remove it from DAG
• Update indegrees, longest path by the formula: longest-path-to(i) = 1 + max{longest-
path-to(j), where (j,i) is a edge in DAG}
• Repeat till all vertices are listed
(a) What is the longest path to 4?

⃝ Option 1: 0 → 1 → 2 → 3 → 4
⃝ Option 2: 0 → 3 → 2 → 1 → 4
⃝ Option 3: 0 → 4 → 3 → 1 → 2
⃝ Option 4: 0 → 2 → 1 → 3 → 4

3. Key Points: Matrix Multiplication

• The (i, j)th element of the matrix C which is product of matrix A and matrix B
where both the dimension of matrix A and B is n × n can be written as:
Cij = nk=1 Aik Bkj
P

• If A is m × n and B is n × p then dimension of AB is m × p.


(a) Find the matrix multiplication of A and B.

⃝ Option 1:

⃝ Option 2:

2
⃝ Option 3:

⃝ Option 4:

(b) Find A2 of the following adjacency matrix A

⃝ Option 1:

⃝ Option 2:

⃝ Option 3:

3
⃝ Option 4:

4. Key Points:
• Weighted shortest path need not have minimum number of Edges.
• Single Sourced Shortest Path: Shortest path from a vertex to every other
vertex.
• All Pair Shortest Path: Shortest path between any two vertex i and j
• Negative Cycle: A negative cycle is one whose weight is negative
• If a graph has a negative cycle, shortest paths are not defined.
• In a weighted graph, the adjacency matrix records the weight where ever there is
an edge and 0 if there is no edge.
• Dijkstra’s Algorithm finds the shortest path between a given node (which is
called the ”source node”) and all other nodes in a graph. This algorithm uses the
weights of the edges to find the path that minimizes the total distance (weight)
between the source node and all other nodes.
1. Answer the following questions based on this graph:
There are six cities A,B,C,D,E and F with time taken to reach from one city to
another is given(in hours)

4
(a) If Aman wants to travel from city A to city F then what is the minimum possible
time it will take for him to reach his destination?
⃝ Option 1: 6 Hours
⃝ Option 2: 8 Hours
⃝ Option 3: 9 Hours
⃝ Option 4: 10 Hours
(b) What is the shortest path between city A to City F?
⃝ Option 1: A → B → C → F
⃝ Option 2: A → C → D → F
⃝ Option 3: A → B → C → D → F
⃝ Option 4: A → C → E → F
Key Points:

• Bellman Ford algorithm helps us find the shortest path from a vertex to all other
vertices of a weighted graph.
• Bellman Ford Algorithm updates distance to each vertex with every iteration
• If Bellman Ford Algorithm does not converge after n-1 iteration then the graph has
a negative cycle.

1. While using Bellman-Ford Algorithm for the graph shown below, let D(v) be the
shortest distance of vertex v from the source vertex after 4 iterations.

(a) Suppose source vertex is A then, which of the following options is correct?
⃝ Option 1: D(B)=3
⃝ Option 2: D(B) = 4
⃝ Option 3: D(D) = 1
⃝ Option 4: D(E)= 7
⃝ Option 5: D(E)=6

5
(b) If the source vertex is changed from vertex A to vertex D. then which of the following
options is (are) correct?
⃝ Option 1: Bellman-Ford algorithm stabilizes after the first iteration.
⃝ Option 2: Bellman-Ford algorithm will not be applicable.
⃝ Option 3: D(C) = 0
⃝ Option 4: None of the Above
Key Points:

• Adjacency Matrix A represent path of length 1.


• A2 [i, j] = 1 if there is a path of length 2 from i to j.
• Floyd-Warshall Algorithm is an algorithm for finding the shortest path between
all the pairs of vertices in a weighted graph. This algorithm works for both the
directed and undirected weighted graphs. But, it does not work for the graphs with
negative cycles
• SP k [i, j] = 1 is the length of shortest path from i to j using the vertices in {1,2,...,k-
1}
• SP n [i, j] = 1 is the length of overall shortest path

1. Use the graph to answer the following questions

(a) Which of the following matrices represents SP 0 ?


⃝ Option 1:

6
⃝ Option 2:

⃝ Option 3:

⃝ Option 4:

Key Points:

• Spanning tree is defined as a subset of a connected undirected graph that has all
the vertices covered with the minimum number of edges possible
• A tree on n vertices has exactly n − 1 edges.
• Adding an edge to a tree creates a cycle.

(a) Which of the following are possible trees of the given graph ?

7
⃝ Option 1:

⃝ Option 2:

⃝ Option 3:

⃝ Option 4:

(b) Which of the following is (are) incorrect with respect to the spanning tree G?
⃝ Option 1: For a graph of n vertices G has n − 1 edges
⃝ Option 2: For a graph of n vertices G has n edges
⃝ Option 3: G is not connected
⃝ Option 4: G is cyclic
Key Points: Prim’s Algorithm

• Incrementally Grow the Minimum Cost Spanning Tree using Prim’s Algorithm
• Start with the smallest weight edge graph
• Extend the current tree by adding the smallest edges from the tree to the vertex
not in the tree

1. Use the graph to answer the following questions

8
(a) If we perform Prim’s algorithm on G, then which of the following options may
represent a minimum cost spanning tree?
⃝ Option 1:

⃝ Option 2:

⃝ Option 3:

9
⃝ Option 4:

(b) Which of the following is the order in which edges are added to the minimum cost
spanning tree?
⃝ Option 1:(1,4),(1,2),(2,3),(1,5),(2,6)
⃝ Option 2: (1,4),(2,3),(1,2),(1,5),(2,6)
⃝ Option 3: (2,3),(1,2),(1,5),(2,6),(1,4)
⃝ Option 4: (1,5),(2,6),(1,4),(2,3),(1,2)
(c) What is the weight of the minimum cost spanning tree?
⃝ Option 1:16
⃝ Option 2:18
⃝ Option 3:19
⃝ Option 4: 17
Key Points: Kruskal’s Algorithm

• Start with n components, each a single vertex


• Process edges in ascending order
• Include edge if it does not create a cylce.

1. Use the graph to answer the following questions

(a) If we perform Kruskal’s algorithm on G, then which of the following options may
represent the minimum cost spanning tree?

10
⃝ Option A:

⃝ Option B:

⃝ Option C:

⃝ Option D:

(b) What is the weight of the minimum cost spanning tree?


⃝ Option 1:16
⃝ Option 2:17
⃝ Option 3:19
⃝ Option 4:18

11

You might also like