Graphs Flashcards

1
Q

What is Bipartite graph ?

A

Whose vertices can be split into two independent groups U,V such that every edge connects between U and V

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

What is complete graph?

A

Where there is a unique edge between every pair of nodes.

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

Three ways of representing the graph?

A
  1. Adjacency Matrix
  2. Adjacency List
  3. Edge List
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which graph representation type is space efficient for

representing dense graphs?

A

Adjacency Matrix

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

Bridges in the graph?

A

A bridge/cut edge is any edge in a graph whose removal increases the number of connected components.

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

Articulation points in graph?

A

Articulation points/cut vertex is any node in the graph whose removal increases the number of connected components.

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

Time complexity of DFS on graph?

A

O(V+E) where V is number of vertices and E is the number of edges

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

Pseudo code for DFS on graphs?

A
# Global or class scope variables
n=number of nodes in the graph
g=adjacency list representation of graph
visited=[false,.....false]# size n
function dfs(at):
if visited[at]: return;
visited[at]=true;
neighbors=graph[at]
for next in neighbors:
    dfs(next)
# start DFS at node zero 
start_node=0;
dfs(start_node)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the time complexity of BFS on graph?

A

O(V+E)
V is number of vertices
E is the number of edges

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

Algorithm to find shortest path on unweighted directed/undirected graphs?

A

BFS(Breadth first search)

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

Pseudo code for BFS on graphs?

A
function BFS(s)
q=queue data structure with enqueue and dequeue
q.enqueue(s)
visited=[false,.....,false] # size n
visited[s]=true;
while !q.IsEmpty():
node=q.dequeue();
neighbors=g.get(node)

for(next: neighbors):
if !visited[next]:
q.Enqueue(next)
visited[next]=true;

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

What are Directed Acyclic Graphs?

A

DAGs are directed graphs with no cycles.

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

Which graph representation is space efficient for representing sparse graphs ?

A

Adjacency List

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

Pseudo-code for DFS on graph

A
# Global or class scope variables
n = number of nodes in the graph
g = adjacency list representing graph
visited = [false, …, false] # size n
function dfs(at):
if visited[at]: return
visited[at] = true
neighbours = graph[at]
for next in neighbours:
dfs(next)
# Start DFS at node zero
start_node = 0
dfs(start_node)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is spanning tree?

A

https://www.ics.uci.edu/~eppstein/161/960206.html
A spanning tree of a graph is just a subgraph that contains all the vertices and is a tree. A graph may have many spanning trees; for instance the complete graph on four vertices

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

What is a minimum spanning tree?

A

https://www.geeksforgeeks.org/kruskals-minimum-spanning-tree-algorithm-greedy-algo-2/

A minimum spanning tree (MST) or minimum weight spanning tree for a weighted, connected and undirected graph is a spanning tree with weight less than or equal to the weight of every other spanning tree.

17
Q

What is topological sort algorithm?

A

https://www.interviewcake.com/concept/java/topological-sort
The topological sort algorithm takes a directed acyclic graph and returns an array of the nodes where each node appears before all the nodes it points to.

18
Q

How to define tree ?

A

Graph with no cycle

19
Q

Which algorithm we can use to detect cycle in graph?

A
  1. DFS
  2. Toplogical Sort
  3. Union Find
    Tarjan’s strongly connected componets
20
Q

How to detect cycle using depth first search in cyclic/acyclic graph?

A
  1. Create color array with three states .
    I. white(not visited), II. Grey(currently visiting) and III. Black(not visited).
  2. Mark all the nodes initially as white.
  3. Do DFS
    Mark current node as grey
    Iterate the neighbors
    if we encounter neighbor with grey color,
    we detect cycle
    else recursively call DFS on neighbors
    After processing all the neighbors , we mark node as
    black(visited)
21
Q

What is the iterative version of DFS on the graph?

A

https: //medium.com/leetcode-patterns/leetcode-pattern-1-bfs-dfs-25-of-the-problems-part-1-519450a84353
1. Initialize node with the stack
2. Pop the current node from stack
3. Mark the current node as visited
4. retrieve all current unvisited neighbors and push them to stack
5. Push all of them to the stack.