Sei sulla pagina 1di 4

Week 08 Tutorial:

Quiz:
1.
2.

7
use man qsort"
there are 6 elements, but only the first 5 are sorted
3.
O(n^2)
- median of three tries to avoid it, but it doesnt prevent there
are still cases
where n^2 behaviour occurs.
4.
man fopen
w: truncate file to 0 length (get rid of file contents)
Q5.
Connected graph:
Complete graph:

vertices are connected, but subgraphs can exist


cliques - every vertex connected to every other vertex

Q6.
Euler path / tour:
(Euler, Edge)
visits every edge in the graph ONCE
Path: no need to end at same vertex you started at.
Tour: end at same vertex you started at. - impossible to get a tour if no
cycle exists.
Hamilton path / tour:
visit every vertex in the graph ONCE
Graph 1: paths only
Graph 2: paths and tours
Graph 3: none
Graph 4: Hamilton path / tour, no Euler path
Q12.
Each node on the map is a vertex, the roads are edges.
Lots of linear algebra in doing graphs.
Q7.
A path exists, no tour (e.g. start from 1). (e.g. 1 5 6 2 3 0 4)
A tour could exist if you add an edge between 1 and 4.
Q8.
A graph has an Euler tour if and only if it is connected and all of its vertices are

of even degree.
Degree: number of edges attached to a node. I.e. all nodes need an even
number of edges
attached to it.
2 vertexes sharing 2 SEPARATE paths with one another: still counts
as degree 2
Intuitive idea: in must be paired with out. Must be even.
Also, needs to be connected, obviously.
Euler path: 2 nodes must have odd degree, (most start at one of them, end at
the other)
Must be somewhere you end at but dont leave.
One way of thinking: a tour with the final edge
removed
linking the end to the front.
(or all nodes are even degree - if you consider an Euler path can be
a tour too).
Function:
typedef struct Vnode *VList;
typedef struct Vnode *VLink;
typedef struct Vnode { Vertex v, VLink next; }
typedef struct Graph { int nv; int nE; VList *edges; }
bool hasEulerTour (Graph g) {
for (int i = 0; i < g.nV; i++) {
if (!connected) return false;
int numEdges = 0; // variable scoped inside the for loop
// more appropriately scoped inside the
loop anyway
for (Vlink curr = g.edges[i]; curr != NULL; curr = curr->next) {
numEdges++;
}
if (numEdges % 2 == 1) return false;
}
return true;
}
Basically:

check connectivity.
for every vertex, count the number of edges.
Above: adjacency list implementation.
bool hasEulerPath (Graph g) {
if (!connected) return false;
int numOddVertices;
for (int i = 0; i < g.nV; i++) {
int numEdges = 0;
for (Vlink curr = g.edges[i]; curr != NULL; curr = curr->next) {
numEdges++;
}
if (numEdges % 2 == 1) numOddVertices++;
}
if (numOddVertices > 2) return false;
return true;
}
Q9.
Some checks we could do:
same number of edges as that in the graph - we need to visit all of them
need to check the end points of each vertex connected by an edge match
up.
cant revisit same edge twice (there are faster approaches)
check the edges we use in the array do exist in the graph (we assume true
here)
typedef struct Edge { int v; int w; }

// stores vertex id start and end

int isEulerPath (graph g, Edge e[], int nE) {


// do number of edges in e[] match number of edges in graph?
if (g.nE != nE) return 0;
// do the edges start and end in the right place?
for (int i = 0; i < nE -1; i++) {
// -1 since we are checking current and
next vertex
if (e[i].w != e[i+1].v) return 0;
// not really that efficient at all - could
merge it all together?
}

// check not used twice


// could use a O(nlog(n)) algorithm, this one is O(n^2)
for (int i = 0; i < nE; i++) {
for (int j = i +1; j < nE; j++) {
if (e[i].v == e[j].v && e[i].w == e[j].w) {
return 0;
} else if // same thing as above if statement, but swap order of i and j
// i.e. we check A -> B, and B -> A
}
}
}
return 1;
}
Exams:

write a function to determine whether some property holds true.


need to know how to work on graphs.
may ask to write an efficient algorithm - O(nlog(n)) or O(n^2) is fine.

Q11.
a) Undirected graph - duplicate entries
b) Directed graph - dont duplicate entries - theres actually less work for us.
from: rows
to: cols
// more natural than the other way around, doesnt matter
though
edges[i][j] > edges[from][to]
Lab Exercise:
sleep(1): includes a delay between each URL access.
if you hammer a server too much, the server will block you.\
also so that you dont DDOS accidentally.

Potrebbero piacerti anche