Home
Explore
Exams
Login
Get started
Home
db
db
0.0
(0)
Rate it
Studied by 0 people
Call Kai
Learn
Practice Test
Spaced Repetition
Match
Flashcards
Knowt Play
Browsing
1/64
There are no tags or description
Looks like no tags are added yet.
65 Terms
View all (65)
Star these 65
1
New cards
What is a graph?
A set of vertices (dots) joined by edges (lines or arrows). Written G = (V, E).
2
New cards
In graph problems, what do n and m stand for?
n = number of vertices. m = number of edges.
3
New cards
What is a directed graph?
A graph whose edges are arrows. Edge (u, v) lets you go from u to v only, not back.
4
New cards
What is an undirected graph?
A graph whose edges have no direction. You can travel both ways along an edge.
5
New cards
What is a path in a graph?
A sequence of vertices where each consecutive pair is joined by an edge you are allowed to follow.
6
New cards
What is a cycle in a graph?
A path that comes back to the vertex it started from. Example: 1 → 2 → 3 → 1.
7
New cards
What is an acyclic graph?
A graph with no cycles.
8
New cards
What does it mean that v is reachable from u?
There is a path from u to v.
9
New cards
When is an undirected graph connected?
When every vertex can reach every other vertex.
10
New cards
What is a connected component?
A largest possible group of vertices that can all reach each other. A graph can split into several components.
11
New cards
What is an adjacency list?
For every vertex, a list of the vertices it has an edge to. Memory: Θ(n + m). BFS and DFS use this.
12
New cards
What is an adjacency matrix?
An n × n grid. A 1 in row u, column v means the edge u → v exists. Memory: Θ(n²).
13
New cards
How much memory does an adjacency list use? An adjacency matrix?
List: Θ(n + m). Matrix: Θ(n²).
14
New cards
Adjacency matrix: how long to check if edge u → v exists?
O(1): just look at one cell.
15
New cards
Why is DFS O(n²) if the graph is stored as an adjacency matrix?
Finding one vertex's neighbors means scanning its whole row of n entries. DFS does that for all n vertices: n × n.
16
New cards
In an adjacency list, how is a directed edge u → v stored?
Once: v appears in u's list only.
17
New cards
In an adjacency list, how is an undirected edge between u and v stored?
Twice: v appears in u's list AND u appears in v's list.
18
New cards
What is BFS (breadth-first search) in one sentence?
Explore the graph in rings: first everything 1 edge from the start, then 2 edges away, then 3, and so on.
19
New cards
What data structure does BFS use?
A queue (first in, first out).
20
New cards
In BFS, what do you do each round?
Take the vertex at the FRONT of the queue and look at each of its neighbors, in the order its adjacency list gives them.
21
New cards
In BFS, what do you do with a neighbor you have never seen before?
Set its distance d = (current vertex's d) + 1, record the current vertex as its parent, and add it to the BACK of the queue.
22
New cards
In BFS, what do you do with a neighbor you have already seen?
Ignore it.
23
New cards
In BFS, what is the distance d of the start vertex?
0
24
New cards
In BFS, what does a vertex's distance d mean?
The fewest number of edges on any path from the start to that vertex (shortest path in an unweighted graph).
25
New cards
In BFS, what happens to a vertex that cannot be reached from the start?
Its distance stays ∞, it has no parent, and it is not in the BFS tree.
26
New cards
What is the BFS tree?
The edges from each vertex to its parent (the vertex that first discovered it). The path from the start to any vertex in this tree is a shortest path.
27
New cards
In BFS, how do you find the actual shortest path to a vertex?
Follow the parent links backward from that vertex until you reach the start.
28
New cards
In BFS, does the order of the adjacency lists change the distances?
No. Distances are always the shortest-path lengths. Only the parents (the tree) can change.
29
New cards
What is the running time of BFS (adjacency list)? Why?
Θ(n + m). Each vertex enters and leaves the queue once (n), and each adjacency list is scanned once (m).
30
New cards
What is DFS (depth-first search) in one sentence?
Go as deep as possible along one path; when stuck, back up to the last vertex that still has an unexplored neighbor and continue.
31
New cards
What data structure does DFS use?
A stack, usually through recursion.
32
New cards
In DFS, what is a vertex's discovery time d?
The clock value at the moment DFS first reaches the vertex.
33
New cards
In DFS, what is a vertex's finish time f?
The clock value at the moment DFS has looked at every neighbor of the vertex and is completely done with it.
34
New cards
In DFS, how does the clock work?
One shared clock, starting at 1. It ticks up by 1 every time a vertex is discovered and every time a vertex is finished.
35
New cards
In DFS, what range do the times fall in?
1 to 2n. Each of the n vertices uses two ticks (one for d, one for f).
36
New cards
In DFS, when does a vertex finish?
When every neighbor in its adjacency list has been checked: new ones explored fully, already-seen ones skipped.
37
New cards
In DFS, in what order do vertices finish along a deep dive?
In reverse. The last vertex discovered finishes first, like closing nested brackets.
38
New cards
In DFS, what is a DFS forest?
If vertices are still unseen after one DFS tree finishes, DFS starts a new tree at an unseen vertex. The result is one or more trees.
39
New cards
In DFS, what are the three states of a vertex?
Unseen (white): never reached. In progress (gray): discovered but not finished. Finished (black): completely done.
40
New cards
In DFS, how can you tell a vertex's state from its times?
No d yet = unseen. Has d but no f yet = in progress. Has both d and f = finished.
41
New cards
What is the running time of DFS (adjacency list)?
Θ(n + m).
42
New cards
What is the difference between d in BFS and d in DFS?
In BFS, d is the distance from the start. In DFS, d is the discovery time on the clock.
43
New cards
In DFS, does the order of the adjacency lists change the times?
Yes. The start vertex and list order can change the d/f times, so follow the order you are given.
44
New cards
In DFS, DFS looks at arrow u → v and v has never been seen. What kind of edge is it?
Tree edge. DFS goes to v and uses this arrow to discover it.
45
New cards
In DFS, what is a back edge?
An arrow u → v where v is an ancestor of u that is still in progress (discovered but not finished). It points back up the DFS tree.
46
New cards
In DFS, what is a forward edge?
An arrow u → v where v is a descendant of u that has already finished (so v was discovered after u: u.d < v.d).
47
New cards
In DFS, what is a cross edge?
An arrow u → v where v has already finished and is in an unrelated branch or an earlier tree (v was discovered before u: u.d > v.d).
48
New cards
In DFS, how do you classify an arrow u → v?
Look at v at that moment. Never seen: tree. Discovered but not finished: back. Already finished: if u.d < v.d it is forward, if u.d > v.d it is cross.
49
New cards
What is the cycle theorem?
A directed graph has a cycle if and only if DFS finds a back edge. (No back edges means acyclic.)
50
New cards
Why does a back edge mean there is a cycle?
v is an ancestor of u, so there is a path from v down to u. The back edge u → v closes the loop.
51
New cards
How do you justify that a graph has a cycle on an exam?
Write: DFS found a back edge u → v, so by the cycle theorem the graph has a cycle.
52
New cards
Do forward edges or cross edges create cycles?
Not by themselves. Only back edges signal a cycle.
53
New cards
How do you find the shortest path (fewest edges) in an unweighted graph?
Run BFS from the start. Read the d values. Follow parents backward for the path.
54
New cards
How do you test whether v is reachable from u?
Run BFS or DFS from u. v is reachable if it gets discovered.
55
New cards
How do you test whether an undirected graph is connected?
Run BFS or DFS from any vertex. If every vertex gets discovered (one tree), it is connected.
56
New cards
How do you find the connected components of an undirected graph?
Run BFS or DFS from an unseen vertex, then repeat from the next unseen vertex. Each tree is one component.
57
New cards
How do you test whether a directed graph has a cycle?
Run DFS. If it finds a back edge, there is a cycle.
58
New cards
What is a bipartite graph?
A graph whose vertices can be split into two sets so every edge connects a vertex in one set to a vertex in the other (no edge inside a set).
59
New cards
How do you test whether a graph is bipartite using BFS?
Run BFS. Put even-d vertices in one set and odd-d vertices in the other. If any edge joins two vertices of the same parity, it is NOT bipartite.
60
New cards
What is the brute-force algorithm for bipartite, and its time?
Try every split of the vertices into two sets (2ⁿ ways) and check all m edges each time: O(2ⁿ · m).
61
New cards
What is a spanning tree?
A tree made of the graph's edges that connects all of the vertices. The BFS or DFS tree edges give one.
62
New cards
What is the running time of all the BFS and DFS applications above?
Θ(n + m).
63
New cards
How do you model a real-world problem as a graph?
Things become vertices. The relationship between them becomes edges (directed if it only goes one way).
64
New cards
Indirect friends in a city: how do you find all maximal groups?
Vertices = people. Edges = friendships (undirected). The groups are the connected components: run BFS or DFS from each unseen person. Θ(n + m).
65
New cards
When should you think BFS, and when DFS?
Shortest path or levels: BFS. Cycles or finish order: DFS. Groups: connected components (either).