Graphs - Chapter 9 Flashcards
A blank is a data structure for representing connections among items, and consists of vertices connected by edges
graph
A blank (or node) represents an item in a graph.
vertex
An blank represents a connection between two vertices in a graph.
edge
For a given graph, the number of vertices is commonly represented as blank, and the number of edges as blank
V
E
Two vertices are blank if connected by an edge.
adjacent
A blank is a sequence of edges leading from a source (starting) vertex to a destination (ending) vertex.
path
The blank is the number of edges in the path.
path length
The blank between two vertices is the number of edges on the shortest path between those vertices
distance
Blank are often used to represent a geographic map, which can contain information about places and travel routes. Ex: Vertices in a graph can represent airports, with edges representing available flights. Edge weights in such graphs often represent the length of a travel route, either in total distance or expected time taken to navigate the route. Ex: A map service with access to real-time traffic information can assign travel times to road segments.
Graphs
A graph can be used to represent relationships between products. Blank in the graph corresponding to a customer’s purchased products have adjacent vertices representing products that can be recommended to the customer.
Vertices
A graph may use a vertex to represent a person. An blank in such a graph represents a relationship between 2 people. In a graph representing a social network, an edge commonly represents friendship. In a graph representing a professional network, an edge commonly represents business conducted between 2 people.
edge
Various approaches exist for representing a graph data structure. A common approach is an blank.
adjacency list
two vertices are adjacent if connected by an edge. In an blank graph representation, each vertex has a list of adjacent vertices, each list item representing an edge.
adjacency list
A key advantage of an adjacency list graph representation is a size of blank, because each vertex appears once, and each edge appears twice. V refers to the number of vertices, E the number of edges.
O(V + E)
A key disadvantage of an adjacency list graph is that determining whether two vertices are adjacent is blank, because one vertex’s adjacency list must be traversed looking for the other vertex, and that list could have V items.
O(V)
in most applications, a vertex is only adjacent to a small fraction of the other vertices, yielding a blank. A balnk has far fewer edges than the maximum possible.
sparse graph
In an blank graph representation, each vertex is assigned to a matrix row and column, and a matrix element is 1 if the corresponding two vertices have an edge or is 0 otherwise.
adjacency matrix
Assuming the common implementation as a two-dimensional array whose elements are accessible in blank, then an adjacency matrix’s key benefit is O(1) determination of whether two vertices are adjacent: The corresponding element is just checked for 0 or 1.
O(1)
A key drawback is blank size. Ex: A graph with 1000 vertices would require a 1000 x 1000 matrix, meaning 1,000,000 elements. An adjacency matrix’s large size is inefficient for a sparse graph, in which most elements would be 0’s.
O(V^2)
An adjacency matrix only represents blank among vertices; if each vertex has data, like a person’s name and address, then a separate list of vertices is needed.
edges
An algorithm commonly must visit every vertex in a graph in some order, known as a blank.
graph traversal
A blank is a traversal that visits a starting vertex, then all vertices of distance 1 from that vertex, then of distance 2, and so on, without revisiting a vertex.
breadth-first search (BFS)
An algorithm for blank enqueues the starting vertex in a queue. While the queue is not empty, the algorithm dequeues a vertex from the queue, visits the dequeued vertex, enqueues that vertex’s adjacent vertices (if not already discovered), and repeats.
breadth-first search
When the BFS algorithm first encounters a vertex, that vertex is said to have been blank.
discovered
In the BFS algorithm, the vertices in the queue are called the blank, being vertices thus far discovered but not yet visited. Because each vertex is visited at most once, an already-discovered vertex is not enqueued again.
frontier
A blank is a traversal that visits a starting vertex, then visits every vertex along each path starting from that vertex to the path’s end before backtracking.
depth-first search (DFS)
An algorithm for blank pushes the starting vertex to a stack. While the stack is not empty, the algorithm pops the vertex from the top of the stack. If the vertex has not already been visited, the algorithm visits the vertex and pushes the adjacent vertices to the stack.
depth-first search
A blank can be implemented using the program stack instead of an explicit stack. The blank algorithm is first called with the starting vertex. If the vertex has not already been visited, the recursive algorithm visits the vertex and performs a recursive DFS call for each adjacent vertex.
recursive DFS
A blank consists of vertices connected by directed edges.
directed graph, or digraph,
A blank is a connection between a starting vertex and a terminating vertex.
directed edge
In a directed graph, a vertex Y is blank to a vertex X, if there is an edge from X to Y.
adjacent
Many graphs are blank, like those representing links between web pages, maps for navigation, or college course prerequisites.
directed
In a directed graph: A blank is a sequence of directed edges leading from a source (starting) vertex to a destination (ending) vertex.
path
In a directed graph: A blank is a path that starts and ends at the same vertex. A directed graph is cyclic if the graph contains a cycle, and acyclic if the graph does not contain a cycle.
cycle
A directed graph is blank if the graph contains a cycle, and blank if the graph does not contain a cycle.
cyclic
acyclic
A blank associates a weight with each edge
weighted graph
A graph edge’s blank, or blank, represents some numerical value between vertex items, such as flight cost between airports, connection speed between computers, or travel time between cities. A weighted graph may be directed or undirected.
weight, or cost,
In a weighted graph, the path blank is the sum of the edge weights in the path.
length
The blank is the sum of the edge weights in a cycle.
cycle length
A blank has a cycle length less than 0
negative edge weight cycle
A blank does not exist in a graph with a negative edge weight cycle, because each loop around the negative edge weight cycle further decreases the cycle length, so no minimum exists.
shortest path
The Graph class holds a vertex adjacency list using a blank that maps a Vertex object to a list of adjacent Vertex objects. The Vertex class contains a label, but can be augmented by graph algorithms to contain additional data if required.
dictionary
A Graph object is initialized with an empty adjacency list. Vertex objects are created and added to the Graph using the blank.
add_vertex() method
Edges are represented as vertex pairs, using a blank.
2-item tuple
Edges also have blank. By default, an edge is assigned with weight 1.0. Edge weights are stored in the dictionary edge_weights where the vertex pair is the key and the edge weight is the value. Ex: The edge (vertex_a, vertex_b) is assigned with weight 3.7 by self.edge_weights[(vertex_a, vertex_b)] = 3.7.
numeric weights
blank traverses a graph by starting at a specific vertex and visiting the vertex’s adjacent vertices before visiting the next closest vertices. No vertex is re-visited.
Breadth-first search
The breadth-first search function has three parameters:
graph is the graph to search.
start_vertex is the starting vertex of the breadth-first search.
distances is an optional dictionary to store the distance of each visited vertex from the start vertex.
The breadth-first search function defines three container variables:
discovered_set is a set of all discovered vertices.
frontier_queue is a Queue variable (Queue class defined in a previous section) that stores vertices that have been discovered, but not yet visited.
visited_list is an ordered list of visited vertices.
Initially, the start vertex is added to both blank and blank. start_vertex is dequeued from the queue and added to the visited list. Then, undiscovered vertices adjacent to the current dequeued vertex are added to discovered_set and frontier_queue. The distances from the adjacent vertices are set to the current dequeued vertex’s distance + 1.
frontier_queue and discovered_set.
The process of dequeuing a vertex from frontier_queue and adding the vertex’s adjacent vertices to discovered_set continues until frontier_queue is empty. In the end, blank contains the order of the breadth-first search’s traversal.
visited_list
Blank search traverses a graph by first visiting a specific starting vertex, and then visiting every vertex along each path originating from the starting vertex. Each path is traversed to the path’s end before backtracking.
Depth-first
To perform depth-first search, the Graph and Vertex classes are used as well as a depth-first search function. depth_first_search() takes three arguments (the current graph, the starting vertex, and a function to mark visited vertices) and defines two container variables:
vertex_stack is a list used as the stack of yet-to-be-visited vertices.
visited_set is a set used to keep track of which vertices have been visited.