Week 8 Flashcards

1
Q

Implementations to store Graphs

A

Advancency List. Each node mapped to a List of adjacent nodes
Edge Set: each node mapped to a set of Edges containing source, destination and weight of edge

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

Graph Travesal Patterns

A

Breadth First

Depth First

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

Breadth First

A

Start with source node
Visist all its neighbors
Then visit all their neihbors
etc. until target node found

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

What does Breadth first algorithm guarantees

A

That we find always shortest path

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

Depth First

A

Start at source node
visit one of its neigbors
then visit one of its neighbor
If node has no neighbors, or all already visited, go back to previous node and visit the next neighbor not visited

	private boolean doDfs(String start, String elementToFind) {
		if (start.equals(elementToFind)) {
			return true;
		} else {
			marked.add(start);
			for (String neighbor : getNodeNeighbors(start)) {
				if (!marked.contains(neighbor) && 
				    doDfs(neighbor, elementToFind))
	             return true;
			}
			return false;
		}
	}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How many times is an edge examined in DFS

A

At most twice for undirected, and at most once for directed

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

Time Complexity of BFS and DFS

A

Both O (V + E)
V = # vertices
E = # Edgeds

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

BFS vs DFS which more efficient

A

Space wise DFS as it only needs to maintained data structure for marked. BFS additionally needs to maintain one for toExplore

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

When use DFS

A

Find directed cycle or identifying SCC

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

Is a cycle C in a graph a valid path?

A

A path is defined as a list of nodes from a start node to an end node in which each successive pair of nodes is connected by an edge. The start and end nodes being the same does not violate the definition of a path.

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

Min number of edges for G to be connected

A

n-1

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

Given an undirected/directed graph G with n nodes, what is the maximum number of edges possible for G if no nodes have self-loops (edges that connect a node to itself)?

A

n(n-1)/2 = undirected

n(n-1) = directed

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