Exam 3 Flashcards
A graph comprises __, each of which holds some kind of __
Vertices, Data
Two vertices can be connected by an __
Edge
Neighbors
Connected vertices in a graph
Graphs are conceptually
Linked-node structures
T/F: An empty graph of no nodes can exist
True
Graphs differ from N-ary Trees and other tree structures in that
-there is no parent/child relationship between nodes
-any node can be connected to any other node
Graphs are ideal data structures for representing systems with ___ between entities
many-to-many
T/F: A binary tree is a type of graph
True. Any formation of nodes is.
Path
A series of edges that connect two vertices
If a path exists between vertices, they are part of the same __
Connected component
Search
An algorithm that determines if a path between vertices exists
Undirected Edges
Edges can be traversed in both directions
Directed Edges
Edges can only be traversed in one direction. Indicated by an arrow.
A graph should provide the following behavior
- Add value to graph
- Boolean check if value in graph
- Size
- Connect directed
- Connect undirected
- Boolean check if two values are connected
T/F: graphs can only be implemented with nodes
False. Can use arrays to get similar result
Adjacency List
Method for vertices to keep track of their unique neighbors. Can be represented by a table:
Vertex | Adjacency List
If no path exists between vertices, they are in __
Different connected components
Breadth-First Search
Path search algorithm. Creates queue with the start vertex. While the queue isn’t empty,
Dequeues next vertex
If value is the goal, return true
Otherwise, add the vertex’s neighbors to the queue (if not previously added)
Nodes should be searched in ___
Alphabetical/numeric order (for this class)
Depth-First Search
Path Finding Algorithm. Uses a queue to search vertices in order based on distance from start. One edge away will be searched first, two, three, etc.
BFS VS DFS
DFS explores deeper and deeper into the graph. BFS explores all nodes one edge away first.
When using DFS, if arrived at a vertex without any unexplored neighbors:
Backtracks along it’s path only as far as necessary to find a vertex with at least one unexplored neighbor
DFS can be implemented with
A stack or through recursion
BFS use a ___ to keep track of predecessors. DFS uses ___
A map. A recursive call.
T/F: BFS guarantees the shortest path
True (fewest edges)
T/F: DFS guarantees the shortest path
False
BFS and DFS time complexity
O(V + E) for both
Algorithm
A finite procedure, written in a fixed symbolic vocabulary, governed by precise instructions, moving in discrete steps, whose execution requires no insight, cleverness, intuition, intelligence, or perspicuity, and that sooner or later comes to an end.
Algorithms often begin as __
Psuedocode
Problem solving by breaking problem down into a series of discrete steps
Algorithmic approach
Psuedocode
High-level descriptions of what the computer will do
T/F: psuedocode should be written with the syntax of the language in mind
False. It should be programming-language agnostic and written in English. There are no grammar or syntax rules.
The purpose of psuedocode
To sketch out an algorithm quickly
A greedy algorithm
Makes decisions based on what appears to be the optimal choice at the time
Local Optimum
Optimal choice at the time
Global Optimum
The best possible answer for the problem
T/F: Greedy algorithms never produce the best possible result for the problem
False. Sometimes they do. Usually they produce a result that is “good enough”
The problem with finding the best answer is that
It can take too long. Greedy algorithms can get close enough much faster
Greedy algorithms make decisions based on
Limited information. No memory of what came before. No idea what comes next.
Weighted graph
A graph that adds a weight to each edge between vertices
Nearest neighbor algorithm
Modification of DFS. Always chooses the neighbor connected by the edge with the lowest weight.
Information Expert
Object-oriented design principle, “behavior follow state.”
If methods are to be added to existing code, they should be added to the classes that contain the necessary data (states)