Graphs Flashcards
What is a graph?
A set of vertices and edges. Each edge connects a pair of verticies
What is a path in a graph
A sequences of vertices connected by edges with no repeated edges
What is a simple path in a graph?
A path (which is a sequence of vertices connected by edges, with no repeated edges) with no repeated vertices.
What is a cycle in a graph?
A path (which is a sequence of vertices connected by edges, where no edge is repeated) with at least one edge. The path’s first and last vertices are the same.
What is a simple cycle?
A cycle (which is a path with at least one edge whose first and last vertices are the same, and where a path is a sequence of vertices connected by edges with no repeated edges) with no repeated vertices. Note that the first and last vertices will be the same.
What is the length of a cycle or path in a graph?
It is the number of edges in a path (vertices connected by edges with no repeated vertices) or a cycle (path with at least one edge whose first and last vertices are the same).
What is a connected graph?
A graph that contains a path (vertices with no rotated edges) between all vertices.
Describe a tree in terms of a graph
An acyclic connected graph (or a graph with no cycles and where every vertex has a path to every other vertex)
What is a spanning tree of a connected graph?
A connected graph is a graph that contains a path from every vertex, to every other vertex. The spanning tree is a subgraph that contains all the vertices of the connected graph. The subgraph is a tree (acyclic and connected graph)
Describe how a depth first search algorithm visits the vertices of a graph
When visiting a vertex
- Mark it as visited
- Visit (recursively) all the vertices that are adjacent to it and that have not yet been marked
What is the time complexity of DFS when marking all nodes?
The time complexity is proportional to the sum of all vertices degrees.
Write a simple DFS algorithm that searches each vertex.
private void dfs(int source) {
marked[source] = true;
count++;
for(int w : adj.get(source))
if (!marked[w]) dfs(w)
Name two problems DFS can solve when looking at graphs
Connectivity: are two different vertices connected? How many connected components does a graph have?
Single-source paths: find a path from source vertex to target vertex
Write a simple DFS algorithm that finds paths from a source to all vertices
Utilize an array called edgeTo[], where edgeTo[w] = v indicates that v-w edge accesses w first in the path. The array is a parent-link representation of the tree rooted at the source. Said another way v is the parent of w.
Private void dfs(int s) {
marked[v] = true;
for (int w : adj.get(s)) {
if (!marked[w]) {
edgeTo[w] = s;
dfs(w);
}
}
}
What is the time complexity of a depth first search algorithm providing a path from a source vertex to a target vertex?
The time complexity is equal to the length of the path.
Name one problem breadth first search solves
This algorithm solves the single-source shortest paths problem. This is equivalent to finding the shortest path (path with fewest edges) from source to some vertex.
How does breadth first search visit each vertex?
This algorithm visits each graph vertex by:
Remove the next vertex from the queue
Put onto the queue all unmarked vertices that are adjacent to the vertex and mark them as seen.
Write a breadth first search algorithm that visits all nodes and records a path from a source to all vertices
Utilize the edge to array to record each vertices parent vertex. Each vertex in the graph is visited first from their parent vertex.
private void bfs(int s) {
Queue<Integer> queue = new Queue<Integer>();
marked[s] = true;
while(!queue.isEmpty()) {
int v = queue.dequeue();
for(int w : adj.get(v)) {
if (!marked[w]) {
edgeTo[w] = v;
marked[w] = true;
queue.enqueue(w);
}
}
}</Integer></Integer>
}
Write a breadth first search algorithm that visits all nodes and records a path from a source to all vertices
Utilize the edge to array to record each vertices parent vertex. Each vertex in the graph is visited first from their parent vertex.
private void bfs(int s) {
Queue<Integer> queue = new Queue<Integer>();
marked[s] = true;
while(!queue.isEmpty()) {
int v = queue.dequeue();
for(int w : adj.get(v)) {
if (!marked[w]) {
edgeTo[w] = v;
marked[w] = true;
queue.enqueue(w);
}
}
}</Integer></Integer>
}
Which graph algorithm finds the shortest path from source vertex s to all vertices?
The breadth first search algorithm finds these paths within a graph
What is the worst case time complexity of breadth first search?
The worst case time complexity of this graph algorithm is Edges+Vertices
What is the worst case time complexity of breadth first search?
The worst case time complexity of this graph algorithm is Edges+Vertices
How can you modify the depth first search algorithm to determine if two vertices are connected?
Modify the depth first search algorithm by assigning a count or id value to every source vertex passed to the DFS method. The count value is a class instance variable. The DFS method is called for every vertex in the graph that has not been marked (visited) yet. Every time a DFS is called, increase the instance count value. This will ensure that every vertex that is reached by the DFS algorithm will be assigned to the same id value. Therefore, all vertices within the same subgraph will have the same id.
How could you modify a simple depth first search algorithm to determine if a graph contains a cycle? Assume the graph has no loops nor any parallel edges.
Modify the depth first search algorithm to check if any adjacent vertices are marked but not the source’s parent vertex. If this is true, set a boolean flag equal to true. When calling the depth first search algorithm, pass the adjacent vertex as the source vertex to examine next, along with the current vertex under investigation as the parent to the source vertex.
How could you modify a simple depth first search algorithm to determine if a graph is two-colorable (bipartite)? Said another way, can you assign two colors to every vertex in the graph such that two adjacent vertices don’t have the same color?
Modify the depth first search algorithm to assign a color to all adjacent vertices that are not marked, with the color opposite of the current vertices color. For marked adjacent vertices, check that the color of the current vertex is different from the color of the adjacent vertex. If not, set the boolean flag to false. Return this boolean flag.
Compare and contrast the marking strategy of the breadth first search algorithm and the depth first search algorithm.
The breadth first search algorthim must mark
What is a directed graph or digraph?
A set of vertices and directed edges. Where each directed edge connects one vertex with another vertex in a particular direction.
What is a directed path?
A sequence of vertices in a digraph with each vertex in the sequence contains a directed edge point from it to its successor. There are no repeated edges.
What is a simple directed path?
A directed path with no repeated vertices.
What is a directed cycle?
A directed path with at least one edge whose first and last vertices are the same
What is a simple directed cycle?
A directed cycle with no repeated vertices (except the requisite repetition of the first and last vertices).
How does depth first search change when solving the reach ability problem (is there a directed path from some source vertex to a target vertex) for a digraph?
There is no change. Instead, the adjacency list which captures all adjacent vertices for each vertex is no longer symmetric.
How does depth first search and breadth first search algorithms change when finding paths in digraphs (compared with undirected graphs)?
There is no change and these methods can solve for single-source directed path (is there a directed path from a source to a target vertex) and single source shortest directed path (if there is a directed path, find the shortest path between source and target vertex).
Define what a topological sort is in relation to a digraph
Order vertices such that directed edges point towards vertices that are farther down in the order. If vertex a points towards vertex b, vertex b must come after vertex a. If you displayed the vertices in order, in a line, from left to right. All edges would point towards the right.
What is a directed acyclic graph?
A digraph with no directed cycles.