graphs_class Flashcards

1
Q

What are standard steps to address any graph problem in general?

A

Build a Graph
Write BFS/DFS
Outer loop to run as many BFS/DFS to explore graph

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

Given list of edge representations (src, dest), how do we build the Adjacency List representation for given undirected Graph

A

For each (src, dest) element within the edge list, we need to perform following two operations
adj_list[src].append(dest)
adj_list[dest].append(src)

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

Given list of edge representations (src, dest), how do we build the Adjacency List representation for given directed Graph

A
For each (src, dest) element within the edge list, we need to perform following operation
adj_list[src].append(dest)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Three requirements/steps to build a graph

A

Need to know vertices of the graph
Build adjacency list for given list of edges using
for each (src, dest) in edges:
adj_list[src].append(dest)
adj_list[dest].append(src)
and
Initialize visited array with blank/null values

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

Why is it preferred to represent vertices in convenient Id format 0 to n-1

A

That would help us using array & use index as the vertex Id, which will eventually enable us to retrieve it’s neighbors in constant time O(1)

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

Basic BFS template for Graphs

A
def bfs(source):
    queue = deque()
    queue.append(source)
    visited[source] = 1
while not queue:
      node = queue.popleft()
      for neighbor in adj_list[node]:
            if visited[neighbor] == 0:
                visited[neighbor] = 1
                queue.append(neighbor)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Basic DFS template for Graphs

A
def dfs(source):
    visited[source] = 1
    for neighbor in adj_list[source]:
         if visited[neighbor] == 0:
            dfs(neighbor)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

DFS tree from graph produces of which type of edges

A

DFS tree from graph produces tree edges and back edges.

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

BFS tree from graph produces of which type of edges

A

BFS tree from graph produces tree edges and cross edges

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

Types of Edges in graphs

A

Tree Edge
Forward Edge
Backward Edge
Cross Edge

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

Outer loop code/logic for graph BFS/DFS traversal

A
components = 0
for v in 0 to n-1:
    if visited[v] == -1:
        components ++
        dfs/bfs(v)
return components
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Outer loop code/logic for graph BFS/DFS traversal

A
components = 0
for v in 0 to n-1:
    if visited[v] == -1:
        components ++
        dfs/bfs(v)
return components
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Sum of degrees of all nodes in an undirected graph

A

twice the number of edges

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

BFS time complexity on Graphs

A

O(m+n) where

O(n) - Push and pop of each vertex from Queue
O(m) - Looking at adjacency list of each vertex

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

Sum of degrees of all nodes in an undirected graph

A

twice the number of edges

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

BFS space complexity on Graphs

A

Auxiliary space: O(n) - number of vertices in queue if source vertex connected to all remaining vertices in worst case
Actual Space: O(n + m)
O(n) - to save vertices information
O(m) - to save edges information

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

DFS time complexity on Graphs

A

O(m+n) where

O(n) - Push and pop out of active stack frame for each vertex
O(m) - Looking at adjacency list of each vertex

18
Q

BFS space complexity on Graphs

A

Auxiliary space: O(n) - number of vertices in queue if source vertex connected to all remaining vertices in worst case
Actual Space: O(n + m)
O(n) - to save vertices information
O(m) - to save edges information

19
Q

DFS time complexity on Graphs

A

O(m+n) where

O(n) - Push and pop out of active stack frame for each vertex
O(m) - Looking at adjacency list of each vertex

20
Q

DFS space complexity on Graphs

A

Auxiliary space: O(n) - number of active stack frame entries is height of the tree, which can be O(n) in worst case
Actual Space: O(n + m)
O(n) - to save vertices information
O(m) - to save edges information

21
Q

When Graph is valid Tree?

A

If all vertices are connected with no cycles in it

22
Q

BFS function to detect if there is a cycle or not

A
def bfs_has_cycle(source):
    queue = deque()
    queue.append(source)
    visited[source] = 1
    parent[source] = None
    while queue:
       node = queue.popleft()
       for neighbor in adj_list[node]:
           if visited[neighbor]  == 0:
                visited[neighbor] = 1
                parent[neighbor] = node
                queue.append(neighbor)
           else: # if visited node
                if neighbor !=parent[node]: # here check if neighbor is not our own parent
                     return True # This is cross edge
return False
23
Q

If there is a cross edge in a tree, can we conclude graph has cycle?

A

Yes

24
Q

DFS function to detect if there is a cycle or not

A

def dfs_has_cycle(node):
visited[node] = 1
for neighbor in adj_list[node]:
if visited[neighbor] == 0:
parent[neighbor] = node
if dfs_has_cycle(neighbor):
return True
else: # if visited node
if neighbor != parent[node]: # here check if neighbor is not our own parent
return True # This is back edge
return False

25
Q

If there is a cross edge in a tree, can we conclude graph has cycle?

A

Yes

26
Q

What type of edge will be created when DFS detects cycle

A

Back Edge

27
Q

In short how do we find if there is a cross edge or back edge

A

If the neighbor vertex is visited and it is not equal to the current vertex parent, we can conclude that graph has cycle.

If we use DFS to traverse the graph there will be Back Edges, where as if we use BFS there will be Cross Edges.

28
Q

What is Bipartite Graph?

A

A Graph is Bipartite if we can split it’s set of nodes into two independent subsets A and B. such that every edge of the graph has one node in A and other node in B.

29
Q

If there are two nodes, without any edges - Is that graph bipartite?

A

Yes. As far as we can put both nodes into two different subsets and no edge connecting to 2 vertices in same sub set, it is bipartite.

30
Q

What is Bipartite Graph?

A

A Graph is Bipartite if we can split it’s set of nodes into two independent subsets A and B. such that every edge of the graph has one node in A and other node in B.

31
Q

If there are two nodes, without any edges - Is that graph bipartite?

A

Yes. As far as we can put both nodes into two different subsets and no edge connecting to 2 vertices in same sub set, it is bipartite.

32
Q

Can we say graph is bipartite if there are cycles?

A

No. there is no relation between both of those characteristics of graph

33
Q

If there is a cycle of odd length, is graph bipartite?

A

No

34
Q

Can I tree bipartite? (Which means graph does not have a cycle?

A

Yes. Every alternate level in tree can be in each subset

35
Q

Can a BFS/DFS tree give information about whether a tree is bipartite or not?

A

Yes.

36
Q

The graph is a bipartite graph if:(Using disjoint sets technique)

A

1) The vertex set of can be partitioned into two disjoint and independent sets.
2) All the edges from the edge set have one endpoint vertex from the set and another endpoint vertex from the set.

37
Q

The graph is a bipartite graph if: (Using coloring technique)

A

A graph is a bipartite graph if and only if it is 2–colorable. While doing BFS traversal, each node in the BFS tree is given its parent’s opposite color. If there exists an edge connecting the current vertex to a previously colored vertex with the same color, then we can safely conclude that the graph is not bipartite.

38
Q

If there are multiple cycles in BFS, with one or more even edge length - Is graph bipartite?

A

Yes

39
Q

If there are multiple cycles in BFS, with one or more odd edge length - Is graph bipartite?

A

No

40
Q

BFS function to detect if connection component is bipartite

A

write code

41
Q

DFS function to detect if connection component is bipartite

A
def dfs_is_bipartite(node):
      visited[node] = 1
      for neighbor in adj_list[node]:
           if visited[neighbor] == 0:
               visited[neighbor] = 1
               color[neighbor] = color (toggling)
               if not dfs_is_bipartite(neighbor):
                    return False
           else:
               if color of neighbor == color of node:
                     return False
       return True   
   (To be tested and filled complete code here)
42
Q

When you have set of inputs and set of pair wise relationships, what category of problem could it be?

A

Graphs