Chapter 38: Graphs and Chapter 63: Graph Traversal Algorithms Flashcards

1
Q

What is a graph

A

A graph is a set of vertices/nodes connected by edges/arcs.

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

Undirected graph

A

All edges are bidirectional

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

Directed graph

A

The edges are unidirectional

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

Advantages compared to Linked List/Binary Tree

A

Each vertex can have more than 2 edges therefore able to point to any vertex in the data structure

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

Weighted graph

A

Where each edge is given an value to show the cost of going from one vertex to another.

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

Adjacency list (define)

A

Adjacency lists: storing graphs as objects or using a dictionary.

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

Adjacency list (python syntax with/without weights)

A

Python syntax (no weights): graph = {“A”:[“B”,”D”,”E”,”F”]…}
Python syntac (with weights): graph = {“A”:[“B”:57,”D”:56,”E”:45,”F”:25]…}

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

Adjacency Matrix

A

Adjacency matrix: a 2d array which stores the value of the weight of the edge between 2 vertices.
Symmetrical with an undirected graph (with 0 top left to bottom right).
Pros: very convenient (adding an edge is simple).
Cons: A sparse graph with many nodes but not many edges will lead to lots of 0s/empty cells therefore inefficient

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

Uses of graphs in computer science

A
  1. Mapping road networks for navigation systems
  2. Storing social network data
  3. Resource allocation in operating systems
  4. Representing molecular structures and geometry.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Types of graph traversals

A

There are 2 ways of traversing a graph:
- A depth-first traversal
- A breadth-first traversal

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

Depth-first traversal

A

In this traversal, we go as far down one route as we can before backtracking and taking the next route.
- Uses an edge-based technique
- Makes use of the stack data structure

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

Depth-first traversal algorithm

A
  1. Set the root node as the current node
  2. Add the current node to the list of visited nodes (if it isn’t already in the list)
  3. Go through all the connected nodes to the current node
    - (i) If the current node isn’t in visited nodes, push it onto the stack. Usually traverse on the left side of the graph
  4. Pop the node from the stack and set it to current node
  5. Start again through step 2.

Once we get to the end of one route, there will be no further nodes to push onto the stack therefore it will pop the next node which is the start of the next route.

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