740 likes | 892 Vues
Assignment 2 Remarking. 2. y = 0.0323x. - 1.5352x + 47.927. Assignment 2 Marks. 2. R. = 0.9999. 100. 90. 80. 70. 60. New Mark. 50. 40. 30. 20. 10. 0. 0. 20. 40. 60. 80. Old Mark. Section V. Graph Algorithms.
E N D
Assignment 2 Remarking 2 y = 0.0323x - 1.5352x + 47.927 Assignment 2 Marks 2 R = 0.9999 100 90 80 70 60 New Mark 50 40 30 20 10 0 0 20 40 60 80 Old Mark
A directed graph G = (V, E), where V = {1,2,3,4,5,6} and • E = {(1,2), (2,2), (2,4), (2,5), (4,1), (4,5), (5,4), (6,3)}. • The edge (2,2) is a self-loop. (b) An undirected graph G = (V,E), where V = {1,2,3,4,5,6} and E = {(1,2), (1,5), (2,5), (3,6)}. The vertex 4 is isolated. (c) The subgraph of the graph in part (a) induced by the vertex set {1,2,3,6}. Directed and Undirected Graphs
Running time often a function of both |V| and |E|. For convenience, drop the | . | in asymptotic notation, e.g. O(V+E). Running Time of Graph Algorithms
Representations: Undirected Graphs Adjacency List Adjacency Matrix
Representations: Directed Graphs Adjacency List Adjacency Matrix
Idea: send out search ‘wave’ from s. Keep track of progress by colouring vertices: Undiscovered vertices are coloured white Just discovered vertices (on the wavefront) are coloured grey. Previously discovered vertices (behind wavefront) are coloured black. Breadth-First Search
Each vertex assigned finite d value at most once. d values assigned are monotonically increasing over time. Q contains vertices with d values {i, …, i, i+1, …, i+1} Time = O(V+E) Breadth-First Search Algorithm
There are no gray nodes. All the black nodes form a connected component in G: WHY? For all the black nodes u, d[u] holds the distance from s to u. Termination + LI Correctness
Idea: Continue searching “deeper” into the graph, until we get stuck. If all the edges leaving v have been explored we “backtrack” to the vertex from which v was discovered. Depth First Search (DFS)
Explore every edge, starting from different vertices if necessary. As soon as vertex discovered, explore from it. Keep track of progress by colouring vertices: White: undiscovered vertices Grey: discovered, but not finished (still exploring from it) Black: finished (found everything reachable from it). Depth-First Search
For any two vertices u and v, exactly one of the following holds: the intervals [d[u], f[u]] and [d[v], f[v]] are disjoint, and neither u nor v is a descendant of the other. the interval [d[u], f[u]] is contained entirely within the interval [d[v], f[v]], and u is a descendant of v. the interval [d[v], f[v]] is contained entirely within the interval [d[u], f[u]], and v is a descendant of u. The Parenthesis Theorem
Vertex v is a proper descendant of vertex u in the depth-first forest if and only if d[u] < d[v] < f[v] < f[u] Corollary
There are four edge types: Tree edges are edges in the depth-first forest Gπ. Edge (u, v) is a tree edge if v was first discovered by exploring edge (u, v). Back edges are those edges (u, v) connecting a vertex u to an ancestor v in a depth-first tree. Self-loops, which may occur in directed graphs, are considered to be back edges. Forward edges are nontree edges (u, v) connecting a vertex u to a descendant v in a depth-first tree. Cross edges are all other edges. They can go between vertices in the same depth-first tree, as long as one vertex is not an ancestor of the other, or they can go between vertices in different depth-first trees. Classification of Edges in DFS
When the edge e=(u,v) is first explored: If v is white – e is a tree edge. If v is gray – e is a back edge. If v is black – e is a forward or a cross edge. Classification of edges
In a depth-first search of an undirected graph G, every edge of G is either a tree edge or a back edge. For Undirected Graphs