Graphs Flashcards

1
Q

A _________________ is a pair V, E ,where V is a finite set and E is a binary relation on V.

A

directed graph

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

TRUE or FALSE:
Edges from a vertex to itself (self- loops) are possible in a undirected graph.

A

FALSE. Only possible for directed graphs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

u -> v

A

u, v is incident from u and is incident to v.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

u -> v

A

v is adjacent to u.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

TRUE or FALSE:
In an undirected graph G = V, E , E
consists of unordered pairs of vertices.

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

u - v

A

u, v is incident on u and v.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

u - v

A

u is adjacent to v and
v is adjacent to u.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In an undirected graph, the ________ of
a vertex is the ________________
incident on it.

A

degree, number of edges

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

In a directed graph:

the ____________of a vertex is the
number of edges leaving it

the _____________ of a vertex is the number
of edges entering it

A

out-degree, in-degree

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

In a directed graph:

the degree of a vertex is the _____ of its in-degree and its out-degree

A

sum

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

A ______ from a vertex v0 to a vertex vk is a sequence of vertices

A

path

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The _____________ of a path is the number of edges in the path.

A

length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

A path is _________ if all vertices in the path are distinct.

A

simple

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Graph Representation using adjacency matrix

A

row is source
column is destination

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

TRUE or FALSE:
In an undirected graph, the adjacency
matrix A is its own transpose: A = AT.

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

An array Adj of V lists, one for each
vertex in V.

Adj[u] consists of all the vertices adjacent to u in G.

A

Adjacency List

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Running time of operations:

Check if vertex u is adjacent to vertex v

A

Adj Mat - O(1)
Adj List - O(V)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Running time of operations:

List all vertices that
are adjacent to u

A

Adj Mat - O(|V|)
Adj List - O(|V|)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Running time of operations:

List all edges of G

A

Adj Mat - O(|V^2|)
Adj List - O(|V| + |E|)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Adjacency lists provide a compact way to represent _____________
i.e. E significantly less than V2

A

sparse graphs

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Adjacency matrices are good for ____________ i.e. E is close to V2.

A

dense graphs

22
Q

Which is better for checking the adjacency between two vertices.

A

Adj Mat

23
Q

________________ visits vertices in waves emanating from the source vertex s.

A

Breadth-first Search (BFS)

24
Q

Starting from source vertex (s), it first visits all neighbors of s

A

BFS

25
Q

BFS uses a ___________ to do this.

A

queue

26
Q

BFS Algo (Pseudo code)

A

bfs(int s, int graphsize){
int j, v, visited[MAXGRAPHSIZE];
queue bfs_queue

for(j = 0; j<graphsize;j++) visited[j] = FALSE

enqueue(s, bfs_queue)

do{
v = dequeue(bfs_queue)
if(!visited[v]){
visited[v] = TRUE
print v
for each vertex j adjacent to v:
if(!visited[j])
enqueue(j, bfs_queue)
}
}while(!isEmpty(bfs_queue))

27
Q

_____________ is similar to the tree preorder traversal.

A

Depth-first search (DFS)

28
Q

The DFS strategy is to _____________ into the graph whenever possible

A

search deeper

29
Q

DFS explores edges out of the ______________________ that still has unexplored edges leaving it.

A

most recently visited vertex v

30
Q

In DFS, once all v’s edges are explored, the search “___________” to the previous vertex and its unexplored edges.

A

backtracks

31
Q

DFS uses a _________

A

stack

32
Q

DFS Algo (Pseudo code)

A

dfs(int s, int graphsize){
int j, v, visited[MAXGRAPHSIZE]
stack dfs_stack

   for(j=0;j<graphsize;j++) visited[j] = FALSE
   push(s, dfs_stack)

   do{
           v = pop(dfs_stack)
           if(!visited[v]){
                  visited[v] = TRUE
                  print v
                 for each vertex j adjacent to v:
                        if(!visited[j])
                             push(j, dfs_stack)
          }
     }while(!isEmpty(dfs_stack))
33
Q

Running time DFS and BFS

A

O(V+E)

34
Q

A ______________ is a subset of the edges that connects all the vertices together without forming a cycle.

A

spanning tree

35
Q

Problem Domain: Find a spanning tree whose sum of all the edge-weights is the minimum

A

Minimum Spanning Tree Problem

36
Q

Algorithms for Minimum Spanning Tree Problem

A

Kruskal’s and Prim’s Algorithm

37
Q

Grows a forest of edges by considering each edge in sorted order by weight

If the edge joins two distinct trees in the forest, it is added to the forest merging the two trees.

A

Kruskal’s Algorithm

38
Q

connect trees via the shortest edge

A

Kruskal’s Algorithm

39
Q

Kruskal’s Algorithm
Pseudo code

A
  1. Sort the edges in increasing order of weight
  2. Starting from the edge with the least weight to the last (or until the vertices are included in the tree): - add the to the MST if it doesn’t form a cycle
40
Q

How to check if the addition of an edge(u,v) will form a cycle in the MST

A

If both u and v belong the the same tree, the edge(u,v) cannot be added to the forest without creating a cycle

41
Q

The tree starts from an arbitrary root vertex and grows until it spans all the vertices in V

Each step adds to the tree an edge that connects it to an isolated vertex, without forming a cycle

A

Prim’s Algorithm

42
Q

maintain a tree by adding the nearest neighbor

A

Prim’s Algorithm

43
Q

Prim’s Algo Pseudo code

A

prims(source):
initialize visited array to zeroes
initialize distance array d to 999
initialize parent array p to -1

d[source] = 0
for number of vertices:
find vertex u, !visited[u] and d[u] is minimum
visited[u] = TRUE
for each vertex v adjacent to u and !visited[v]
distance = weight(edge(u,v))
if distance < d[v]:
d[v] = distance
p[v] = u

44
Q

To get the MST in Prim’s Method, get the edges (parent, vertext)

A

To get the total edge weight of the MST, take the summation of the distance array

45
Q

A shortest path from vertex u to vertex v is any path p that has the minimum weight (or shortest path length if unweighted)

A

Single-Source Shortest Path Problem (SSSP)

46
Q

Given a graph G = (V,E) find a shortest path from a given source vertex s ∈ V to every vertex v ∈ V.

A

SSSP

47
Q

Dijkstra’s Algorithm Pseudo code

A

dijkstra(source):

initialize visited array to zeroes
initialize distance array d to 999
initialize parent array p to -1

d[source] = 0
for number of vertices:
find vertex u, !visited[u] and d[u] is minimum
visited[u] = TRUE

  for each vertex v adjacent to u and !visited[v]
        distance = d[u] + weight(edge(u,v))
        if distance < d[v]:
               d[v] = distance
               p[v] = u
48
Q

Lemma: A shortest path between two vertices ___________________________ within it.

A

contains other shortest paths

49
Q

To get the shortest path, trace ______________ from the destination vertex to the source vertex (using parent Array)

A

BACKWARDS

50
Q
A