Graph Flashcards

1
Q

Graph Types

A

Undirected
Directed
- Directed Acyclic Graph ( DAG)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Graph Data structure

A

Adjacency Matrix
- M/N array - ‘1’ represents edge between 2 vertices i and j.
Adjacency List
- Array of List holding edges for each vertex.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Graph Algorithms

A
BFS
DFS
Union-Find
Single Source Shortest Path
   - Dijkstras 
   - Bellman Ford
All pair shortest path
   -  Floyd Warshall
 Minimum Spanning Tree
  - Prim's
  - Kruskals
Topological Sorting
BackTracking
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

BFS

A
  • Use Queues to traverse the graph.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

DFS

A
  • Recursion stack
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Disjoint Sets

A
  • To find cycle in an undirected graph.
    Use Find and Union to find and detect the cycle.
  • Prim’s and Kruskal algorithms use disjoint sets to detect the cycle.

Find: finds a vertex in a set.
Union: merges set s1 and set s2, if U and V are belongs to S1/S2.

if both vertices are in same set, then there is a cycle.

Array Representation of disjoint sets:

Initialize array with -1 for all vertices. As we find edge between vertices, change its value to parent and parent edge being negative number(represents number of nodes). If vertices parent are different, merge using union. Make one set parent of other set and change the number of elements.

Weighted Union (Union by Rank):
In union, select the parent based on the number of elements and there will be a hierarchy of underlying children and its parents.
set1: { 1, 2} s2: {3, 4}
-1 3-> 1 -> -1.
Collapsing Find:
Here we can make all children point directly to main parent. This makes constant time to find the direct parent of any child.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Minimum Cost Spanning Trees

A

Spanning tree - is a subset of a graph where all vertices are connected and does not form a cycle.
Given V vertices and E edges,
Spanning tree will have V vertices and V-1 edges.

Minimum cost spanning tree - in weighted graph subset of a graph with minimum cost ( no cycles).
Algorithms:
Kruskal
Prims

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Prim’s and Kruskal ( Greedy approach)

A

Prim’s Algorithm:
Select minimum cost edge and from the selected edge continue selecting the minimum cost edge. Next minimum edge should be always selected from already selected vertex.

Time complexity: O(n 2)
N- no. of vertices
N - no.o f edges.

Kruskals’s Algorithm:
Select the minimum edge even though if it is not connected. Select the edge if it is not forming a cycle.
- Min Heap can be used to select the edge.
Kruskal’s algo works for non-connected graphs also. But it may not provide the spanning tree.

Complexity: O(n * logn)
Log n to select the edge using min heap; n - no of vertices.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Cycle Detection - (Undirected Graph)

A

Union-Find Algorithm
BFS
DFS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Cycle Detection - (Directed Graph)

A

Using indegrees/outdegrees of Vertex
BFS
DFS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Find Connected components ( Directed Graph)

A

Kosaraju’s Algorithm

Tarjan’s Algorithm

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Topological Sorting

A

Given a directed (Acyclic) graph - edge between vertices U-> V represents edge U must be processed/visited before proceeding to Vertex V.
Topological sorting is useful in task scheduling with prerequisites.

Algorithms:
Kahn’s Algorithm
DFS

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Kahn’s Algorithm

A

In-degree - Vertex with incoming edges
Out-Degree - Vertex with outgoing edges.

  1. Start with vertex having no incoming edges. (also called source)
  2. Add current source vertex to result list.
  3. visit all of its outgoing vertices and add it to queue.
  4. Decrement in-degree of child
How well did you know this?
1
Not at all
2
3
4
5
Perfectly