Programming Fundamentals - Graphs
Describe the Recursive DFS Process.
- Base case: Pick an edge and mark a node as seen. - Changing input: Node - Escape: When every node is seen, end the process.
What is the difference between the graph DFS stack and BFS queue?
- DFS stack - LIFO, so the next element you look at was the last element you saw. - BFS queue - FIFO, so the next element you look at was the first element you saw.
What are some special cases with Adjacency Matrices?
- There is always a 0 diagonally because there cannot be a connection between a node and itself. - There are always two 1's for every connection.
What is the difference between traverse and search?
- Traverse - You look at every element. - Search - You traverse until you find the element you are looking for.
What are the main types of graph traversal?
1. Depth-First Search (DFS) 2. Breadth-First Search (BFS)
What types of lists can be used to represent connections on simple graphs?
1. Edge List 2. Adjacent List 3. Adjacent Matrix
What is a directed graph? Give an example.
A graph whose edges can have direction. Twitter is directional.
What is an undirected graph? Give an example.
A graph whose edges cannot have direction. Facebook as undirected.
What is an Edge List?
A list which contains lists of connections. Each list shows two nodes that have an edge between them.
What is connectivity?
A measurement that measures the number of elements that need to be removed for a graph to become disconnected.
What is an Eulerian Cycle?
A path that traverses every edge only once and ends up with the same node you started with.
What is an Eulerian Path?
A path that traverses every edge only once, but does not have to end on the path you started with.
What is a path?
A specific route you take in a traversal, or search.
What is an Adjacency Matrix?
A square, whose vertical and horizontal indices represent the different nodes, where 1 is input as a connection and 0 is input as not a connection.
What must be true of a graph to have an Eulerian Path?
All nodes must have an even degree, or number of edges, except for the start and end points of the graph, which can have an odd degree.
What must be true of a graph to have an Eulerian Cycle?
All nodes must have an even degree, or number of edges.
What is a graph (network)?
An abstract data structure designed to show relationships between objects.
What is a disconnected graph?
An unconnected graph has a vertex or group of vertices that can't be reached by other vertices.
What is an Adjacency List?
Each index refers to a node. In each index is stored a list of nodes that the index is connected to.
Describe how a Graph BFS can be viewed as a tree.
The first element you look at is the root. You then look at all adjacent elements, which become the next layer in a tree.
Describe the Graph BFS Process.
1. Pick a node to begin with 2. Mark that node and add it to the queue 3. Go back to the first node and visit everything adjacent to it. 4. When we run out of edges, we can dequeue the root node from the queue and use the next node as the starting point.
Describe the Graph DFS Process.
1. There is no root, so begin with any node. 2. Use a stack to keep track of the nodes you have checked. 3. Mark the node you selected as seen and store it in the stack. 4. Repeat these steps as long as there are more unseen edges and nodes. 5. If you do hit a node you've seen before, go to a previous node and try another edge. 6. If you run out of new edges, pop the node from the stack and go to the previous node in the stack.
What is a connected graph?
A connected graph has no disconnected vertices.
What is a cycle?
A cycle is a loop, or circuit in the graph.
What is a Directed Acyclic Graph (DAG)?
A directed graph that has no cycles.
Are Eulerian Paths/Cycles always possible with graphs?
No.
What is the efficiency of graph BFS
O(n), because every node and edge have to be explored.
What is the efficiency of graph DFS?
O(n), because every node and edge have to be explored.
What dimension are these lists? Why?
These lists are 2D because they are lists within lists.
Why are graphs used as inputs in code often acyclic?
They are acyclic to avoid infinite loops.
Describe the similarities and differences between trees and graphs.
Trees are minimally connected graphs that only show one path between any two vertices. Graphs can have more than one path with uni or bidirectional edges between nodes.
Can edges store data? If so, what type?
Yes, they often store the strength of the connection, such as how many times in the past week we talked to each other.