Part 6 Flashcards

1
Q

How would the expression (a+7)*b be represented in a tree?

A

times

/ \

plus id(b)

/ \

id (a) number(7)

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

What is a parser?

A

A program that converts text into parse trees

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

What two pieces of information does a node in a parse tree need to hold?

A

Type of node, i.e. number, identifier, Operator etc.

Value, e.g. 7, myId or +

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

What are the two approaches for parse tree nodes in Java?

A

Use Object to store the data

write an abstract class and subclasses for each kind of node

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

What is a graph?

A

A collection of vertices and edges where each edge connects two of the vertices

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

What is the difference between a directed and undirected graph?

A

Directed - edges have direction so (u, v) is not the same as (v, u)

Undirected - edge has no direction so (u,v)==(v,u)

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

What is a path from vertex v1 to vk?

A

A sequence of edges, e.g.

(v1, v2), (v2, v3), …, (vk-1, vk)

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

What is a connected graph?

A

Every pair of vertices u and v has a path from u to v

I.e. you can go from any vertex to any vertex, somehow

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

What is a cycle (in graphs)?

A

A non-empty path from a vertex to itself comprising distinct edges

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

What is an acyclic graph?

A

A graph that contains no cycles

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

What is a weighted graph?

A

A graph where each edge has some numeric value associated with it, i.e. as a measure of distance, cost or capacity

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

Which letters denote the number of vertices and edges in graphs?

A

n = number vertices

e = number edges

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

For any graph, what is e less than?

A

n^2

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

For any undirected graph, what is e less than?

A

n^2 / 2

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

For a connected graph, what is e greater than or equal to?

A

n-1

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

For an acyclic graph, what is e less than?

A

n

17
Q

If a graph is connected and acyclic what is e equal to in terms of n?

A

n-1

18
Q

What are the two most common methods for representing edges in a graph?

A

Adjacency matrix

Adjacency graph

19
Q

How does an adjacency matrix work?

A

array of boolean[u][v]

cell [u][v] has the value true iff there is an edge (u,v)

20
Q

How does an adjacency list work?

A

For each vertex v, a list of its neighbours (with weights if appropriate) stored in a master list

21
Q

How many entries does an adjacency matrix contain?

A

n^2

22
Q

How many entries does an adjacency list contain?

A

n+e

23
Q

When is an adjacency list better than a matrix in terms of space?

A

When the graph is sparse

24
Q

What is a spanning tree for a connected undirected graph?

A

An acyclic connected graph whose vertices are those of the original graph and whose edges form a subset of those of the original graph.

I.e. connection between every vertex without cycles

25
Q

What is the cost of a spanning tree in a weighted connected graph?

A

The sum of all the weights of the edges in the spanning tree

26
Q

What is a minimum cost spanning tree?

A

The spanning tree for a graph with the lowest cost

27
Q

How does prims algorithm work?

A

Randomly select a vertex from the graph

while(not complete)

add the lowest cost edge connecting a connected vertex and non connected vertex to the mcst

28
Q

How many times is the body of prims algorithm executed?

A

n-1

29
Q

How is prims alrogithm made efficient?

A

For each vertex keep a record of its lowest cost connected neighbour by storing the cost. If the vertex is connected itself, put a cost of 0 and if no neighbours, -1

When an edge is added to the MCST need to update entries

30
Q

What is the time complexity of prims algorithm? Why?

A

O(n2)

Finding the lowest cost is O(n), and updating the list is O(n) so the whole loop body is O(n2)

Initialisation is O(n)

31
Q

How does kruskal’s algorithm work?

A

select the lowest cost edge from the graph

Until there are n-1 edges in the MCST:

if it won’t make the MCST cyclic, add it to the mcst

Select the next lowest edge…

32
Q

How is MCST made efficient?

A

Easily determine whether the MCST will be acyclic after adding an edge:

maintain collection of connection sets (sets of vertices that form connected subgraphs in the incomplete MCST) - if the two vertices of the new edge are both in the same connection set a cycle will be introduced

33
Q

What is the time complexity of Kruskal’s algorithm?

A

O(eloge)

34
Q

What does Dijkstra’s algorithm do?

A

Determines the shortes path from one vertex to another

35
Q

What two sets does Dijsktra’s algorithm maintain?

A

Known set - vertices to which the shortest path has been found

Frontier set - vertices to which there is an edge from one of the vertices in the known set

36
Q

What happens at each step of Dijsktra’s algorithm?

A