300 likes | 610 Views
Algorithm Fundamentals. Definition Steps in Construction Properties. Definition and role of Algorithms. Algorithms are central to computer science An algorithm to solve problem P is a sequence of instructions sequence is finite each instruction is unambiguous
E N D
Algorithm Fundamentals Definition Steps in Construction Properties
Definition and role of Algorithms • Algorithms are central to computer science • An algorithm to solve problem P is • a sequence of instructions • sequence is finite • each instruction is unambiguous • initial instructions acquire valid input • intermediate instructions perform processing • final instructions produce valid output for P • algorithm terminates after finite number of steps
Algorithm Issues • Algorithm must be specified • Pseudocode is combination of English and programming language constructs • Algorithm is either exact or approximate • An exact algorithm produces an output which is valid for input while an approximate algorithm produces an output close to valid output
Algorithm Issues • Algorithm should be efficient • If input is size n, time complexity of algorithm may depend only on n (every case) or may depend on particular input (worst case / best case) – will use Big Oh notation • Algorithm should be tested • Before implementation by hand example • After implementation by run time scenarios • Algorithm is implemented in programming language P • Each module and major control statement of P should be commented
Algorithm Examples • Euclidean Algorithm • Problem : Find greatest integer d which divides m and n • Fact : If m ≥ n then m = d x n + r , 0 r < n • Fact : If p is a common divisor of m and n then • m = p x k1 and n = p x k2 and • r = m – d x n = p x k1 – d x p x k2 so p is a common divisor of r • Exchange problem of finding greatest integer which divides m and nfor smaller problem of finding greatest integer which divides n and m mod n • Example : Find gcd(96,36) • gcd(96,36) = gcd(36, 96 mod 36 = 24)
Euclidean Algorithm Pseudocode • Euclid(m,n) • Computes gcd(m,n) • Input : Two integers m, n with m > n ≥ 0 • While n 0 • r m mod n • m n • n r • Return m • How do we know Euclid’s algorithm terminates?
Euclidean Algorithm Example • Euclid(96,36)
Programming Assignment # 1due Sept 16 Find gcd(m,n) • Using Euclidean Algorithm • Measure time complexity by number of divisions • Using • sieve of Erastothanes • Finding prime factors of m and of n • Measure time complexity by number of divisions • Finding lowest exponent on each prime factor • Measure time complexity by number of comparisons
Fundamental Data Structures • Arrays – sequence of n items of same data type with length. Each item is accessed by starting location + index. • Strings – sequence of characters. Implemented as array. Dynamic strings end with special character. • Linked List – sequence items implemented as item value with link to next item. • Stacks – list in which insertions and deletions are made from same end of list (the top) • Queues – list in which insertions are made one end (tail) and deletions are made from other end (head). • Graphs – a set of vertices and a set of vertex pairs called edges.
Graph Representation • List of vertices and list of edges. • Edge List : {(1,3), (2,1), (2,3), (3,2)} • Two dimensional array (adjacency matrix) with boolean entries or edge length entries. • _ 1 2 3 • 1 - - 1 • 2 1 - 1 • 3 - 1 - • Directory plus neighbor list – directory has start and stop into neighbor list • Directory Neigbor List • Start Stop • 1 1 1 1 3 • 2 2 3 2 1 • 3 4 4 3 3 • 4 2
Weighted Graphs and Paths • A weighted graph is a graph in which each edge e has an associated weight we. The graph is symmetric if the weight of (u,v) = weight of (v,u). • A path is a sequence of vertices v1, v2 .. Vn for which (vi , vi+1) is an edge for each i. • The length of a path with edge weights w1, w2 .. Wn is the sum of the edge weights - wi
Graph Properties • Connected – a graph is connected if there is a path between every pair of vertices. • A cycle (circuit) is a path for which start and end vertices coincide. • A tree is a graph which is connected and has no cycles.
Tree Properties • A tree with n vertices has n-1 edges. • Proof by induction on the number of vertices • If n = 1, there is 1 vertex and 0 edges since a tree by definition has no cycles and thus no loops. • If a tree with n vertices has n – 1 edges, let T be a tree with n + 1 vertices. Pick a vertex v in T and if it’s degree is greater than one, move to a neighbor v’ along edge e. If the degree of v’ is greater than one, move to a neighbor v’’ along an edge e’ different from e. Eventually must arrive at a vertex z with degree 1. Why? Remove z and the edge by which we reached z. The result is a tree T’. Why? T’ has n vertices and by inductive hypothesis, must have n-1 edges. Therefor T has n = (n+1) – 1 edges.
Sets and Dictionaries • A set is a collection of objects. Objects belong to a set at most once. The empty set is a set with no elements or objects • A dictionary is a structure which has key objects and a unique value associated with each key.
Graph theory: The city of Königsberg • The city is divided by a river. There are two islands at the river. The first island is connected by two bridges to both riverbanks and is also connected by a bridge to the other island. The second island has three bridges connecting to a riverbank or the other island. • Question: Is there a walk around the city that crosses each bridge exactly once? • Swiss mathematician Leonhard Euler invented graph theory to solve this problem.
Euler Circuits • A graph has an Euler circuit iff it is connected and every vertex is of even degree. • Necessity – Euler circuit enters a vertex each time on a new edge and leaves the vertex on a new edge. So vertex has degree 2 * number of times on circuit • Sufficiency – Pick starting vertex and traverse graph, each time picking new edge. Can only be blocked at start. Either have Euler circuit or can pick vertex with unused edge on circuit and build subtour starting with it. Splice subtour in. Eventually will have used all edges once.
Service Delivery : mail delivery, meter reading etc. require visiting each street of a city.
Programming Assignment 2 • Find minimal Euler tour of an undirected graph • Read number of vertices and edge list (u,v) • Determine odd vertices (even number of them) • Determine all pairs of vertex distances • Pair odd vertices to minimize the sum of paired distances • Double shortest paths connecting paired odd vertices • Find Euler tour of resulting Euler graph
An undirected weighted graph 1 2 5 3 4 6
Dynamic ProgrammingAll Pairs Shortest Path Dk(i,j) = min{ Dk-1(i,j) , Dk-1(i,k) + Dk-1(k,j) } This is decomposition of problem to shortest path through vertices 1..k is min of going through vertex k and not going through vertex k