Unit 7: Data Structures III
size:
returns the number of values in the graph.
connected(Ea, Eb):
returns true if the two values are connected in the graph, and false otherwise.
contains(E value):
returns true if the value is present in the graph, and false otherwise.
What is a graph?
A graph is conceptually a linked-node structure containing at least one node with a value of some type and a list of nodes to which is is connected.
A path is ____
A series of edges that connects two vertices together
How do you keep track of visited nodes?: A Set Just do it by hand A List A Node
A set
What two parts comprise a node?
A value of some generic type, and a reference to the next node
Which is not an ending condition for a BFS?: The target vertex has been found All the vertices connected to the starting one have been visited The queue has emptied A vertex has no neighbors to add to the queue
A vertex has no neighbors to add to the queue
How does BFS order visits?
All neighbors first
A connection between two vertices is called _______
An edge
What happens if you call DFS without having it implemented? You write the code for it then and there A default implementation of it is used An exception is thrown
An exception is thrown
What is the difference between BFS and DFS?: DFS doesn't find all neighbors DFS always finds the shortest path using recursion DFS uses a queue BFS uses a queue to always finds the shortest path
BFS uses a queue to always finds the shortest path
When the edge between two nodes is undirected, it works in ____
Both directions
What real-world systems could be represented by a graph?
Computer networks, cities connected by highways, airports connected by airline routes
An abstract data type (ADT) _____
Defines the behavior of a data structure from the perspective of its user, but does not provide any implementation details.
For example, the edge connecting vertex A to vertex B may be identified as ____
EAB
How does an Adjacency list (Graph) work?
Each vertex keeps track of the other vertices to which it is connected in some data structure.
What is the connection between two vertices in a graph called?: Path Edge Link Connection
Edge
A BFS algorithm, at a vertex, only adds one neighbor to the queue at a time. True or False?
False
How does DFS order visits?
One neighbor, then neighbor of neighbor alphabetically
If two vertices are connected by an edge, they are ____
Neighbors
Does DFS guarantee the shortest path?
No
Which of these isn't a term used with graphs?: Nodes Vertex Neighbors Edge
Nodes
What is the time complexity of both BFS and DFS
O (V + E)
When the edge between two nodes is directed, it works in ____
One direction
What data structures does BFS use?
Queue and map
How does DFS work?
Rather than explore all of the nodes that are one edge away first, DFS explores deeper and deeper into the graph. If it arrives at a vertex that does not have any unexplored neighbors, it backtracks along its path only as far as necessary to find a vertex with at least one unexplored neighbor.
What data structures does DFS use?
Stack (with recursion)
A cycle occurs in a graph traversal when you end up back at the starting vertex. True or False?
True
What is the difference between a directed and undirected edge?: The directed edges are 2 ways, while undirected edges are 1 way The directed edges are 1 way, while undirected edges are 2 ways There is none
The directed edges are 1 way, while undirected edges are 2 ways
What separates a graph from an N-ary Tree?
There is no "parent/child" relationship between nodes in a graph. Any node in the graph may be connected to any other node.
Which has a better time complexity, DFS or BFS?: DFS BFS They're the same
They're the same
How do you avoid cycles in BFS algorithms?: Hard code them to skip over the starting vertex Use a set to keep a unique list of visited vertices Give up if you find a cycle Change the queue to never store the vertex being visited
Use a set to keep a unique list of visited vertices
Each node in a graph is called a _____
Vertex
How does the DFS path builder work?
Vertex V makes a recursive call for each of is neighbors N. If the recursive returns null, that means that there is no path to E that includes N. If the recursive call returns a non-null path, e.g. a list with at least one other vertex in it, that means that a path to E exists, and it includes N.
Are linked lists and binary trees types of graphs?
Yes
Can a graph contain a single edge?
Yes
Can a graph contain a single vertex?
Yes
Does BFS guarantee the shortest path?
Yes (Fewest edges)
What would be a legitimate con of using an ArrayList to store the neighbors in a vertex?: You can't easily add items to an ArrayList You can't easily check if an item is in an array list (ie in O(1) time) You can't iterate over an ArrayList
You can't easily check if an item is in an array list (ie in O(1) time)
How does DFS work?: You visit a node, then it's first neighbor, then it's first neighbor, and so on, then backtrack recursively when you need to. You don't I have no idea man You visit every neighbor for each node, then move on
You visit a node, then it's first neighbor, then it's first neighbor, and so on, then backtrack recursively when you need to.
add(E value):
adds a value to the graph
connectDirected(E a, E b):
creates a directed connection between the specified two values in the graph.
connectUndirected(E a, E b):
creates an undirected connection between the specified two values in the graph.