博文

目前显示的是标签为“Data Structure and Algorithm”的博文

[ACE] Graphs

图片
Graph Definition of a graph A graph is a set of nodes, or vertices, connected by edges. Graphs can be undirected - edges don’t have direction directed - edges have direction Undirected graphs can be represented as directed graphs where for each edge (X,Y) there is a corresponding edge (Y,X) . Graphs can also be unweighted weighted - edges have weights Notation Set V of vertices (nodes) Set E of edges (E⊂V*V) Node B is adjacent to A if there is an edge from A to B.(A —–>B) Paths and reachability A path from A to B is a sequence of vertices A1,…,An such that there is an edge from A to A1, from A1 to A2,… from An to B Vertex B is reachable from A if there is a path from A to B Other Terminology A cycle is a path from a vertex to itself Graph is acyclic if it does not have cycles Graph is connected if there is a path between every pair of vertices Graph is strongly connected if there is a path in both directions between every pair of vertices how t...

[ACE] Amortized analysis

图片
Amortized analysis Why talking about amortized analysis When we are learning “Vector” in ACE class, we came up with a growable array-based vector. We want to know what if we execute “push” operation, but the array is full. Instead of throwing a exception, we would like to replace the array with a larger one Algorithm push(o) if t = s.length -1 then A <- new array of size ... for i <- 0 to t do A[i] <- S[i] t <- t+1 A[t+1] <- o garbage collection S From pseudo-code above, what should be the size of the new array? There are two simple strategy you can came up with quickly • incremental strategy: increase size by a constant c • doubling strategy: double the size What is the common analysis for this question? Suppose some individual operation (such as ‘push’) takes time T in the worst-case. So for the total s operations, the worst-case time should be sT(upper bound) • however, such an upper-bound might not ever occur. Because the...

[ACE] Recurrence Relation

图片
Recurrence Relation Acknowledgement The following content is summarized by G52ACE class and MIT open source course(Introduction to Algorithms). Main Body We don’t care the official definition of recurrence relation, but we need to know: Suppose that the runtime of a program is T(n), then a recurrence relation will express T(n) in terms of its values at other (smaller) values of n. Example: Suppose the runtime of merge-sort of an array of n integers is T(n). Then T(n) = 2 T(n/2) + b + a n: • “2 T(n/2)” is due to having to sort the two sub-arrays each of size n/2 • “b” is the cost of doing the split • “a n” is the cost of doing the merge (and any copying to/from the workspace, etc.) Also, what is our base case? ===> T(1) = 1 Thus, how can we solve this recurrence relation? Solving Recurrence using induction(introduced by G52ACE course) General pattern 1. Starting from the base case, use the recurrence to work out many cases, by directly substituting and working u...

[ACE] Simple Sorts

图片
Simple Sorts There are a lot of simple sorting algorithm, in this blog article, we’ll be looking at bubble sort, selection sort and insertion sort. Bubble Sort Basic Idea: There are two loops, the outer loop repeatedly scans through array and the inner loop do comparison with immediate neighbour. Implementation: import java.util.Arrays; public class BubbleSort { public static void main(String[] args) { int array[] = {23,10,12,5,14,14}; bubbleSort(array); System.out.println(Arrays.toString(array)); } public static void bubbleSort(int array[]){ for(int i = array.length-1; i > 0; i--){ for(int j = 0; j < i; j++){ if(array[j] > array[j+1]){ int temp = array[j]; array[j] = array[j+1]; array[j+1] = temp; } } } } } Stability: For this kind of implementation, it is stable. Complexity: For an array of si...

[ACE] Quick Sort

图片
QuickSort In Merge Sort, the ‘divide’ is simple and the ‘merge’ is relatively complicated. Can we make the ‘merge’ simple? Yes, and to make ‘merge’ becomes concatenate. Concept Quick-sort is a randomized sorting algorithm based on the divide-and-conquer paradigm: Divide: pick a random element x (called pivot) and partition S into L(elements less than x), E(elements equal to x) and G(elements greater than x) Recur: sort L and G Conquer: join L, E and G implement using Java import java.util.Arrays; public class QuickSort { public static void main(String[] args) { int [] a = {5, 2, 4, 7, 1, 3, 2, 6}; quick_sort_v1(a, 0, a.length-1); System.out.println(Arrays.toString(a)); quick_sort_v2(a, 0, a.length-1); System.out.println(Arrays.toString(a)); } /* * This version of quick_sort is recursively copy the value of i to j then copy j to i. */ public static void quick_sort_v1(int a[], int l, int r){ int i...

[ACE] Merge Sort Analysis

图片
MergeSort Divide and Conquer Divide and Conquer is a general algorithm design paradigm: Divide: divide the input data S in two disjoint subsets S1 and S2 Recur: solve the subproblems associated with S1 and S2 Conquer: combine the solutions for S1 and S2 into a solution for S Merge-Sort and Quick-Sort both use divide and conquer method. MergeSort Merge-sort on an input sequence S with n elements consists of three steps: • Divide: partition S into two sequences S1 and S2 of about n/2 elements each • Recur: recursively sort S1 and S2 • Conquer: merge S1 and S2 into a unique sorted sequence We first think about “conquer” step, to merge halves of an array, we need to traversal every element, so the runtime is O(n) Implementation: public static void recMergeSort(int [] arr, int [] workSpace, int l, int r){ if(l == r){ return; }else{ int m = (l + r) / 2; recMergeSort(arr, workSpace, l, m); recMergeSort(arr, workSpace, m+1, r); ...

[ACE] Binary Search Tree

图片
Tree Terminology Root: node without parent Internal node: node with at least one child External node: node without children Depth: number of ancestors(not counting itself)/ number of lines from the node to the root Height: maximum depth of any node/ length of longest path from root to a leaf Binary Search Tree and its implementation Binary Search Tree(BST):  A binary search tree is a  rooted   binary tree , whose internal nodes each store a key (and optionally, an associated value) and each have two distinguished sub-trees, commonly denoted  left  and  right . The tree additionally satisfies the binary search tree property, which states that the key in each node must be greater than all keys stored in the left sub-tree, and not greater than all keys in the right sub-tree.   Simply speaking, it is a binary tree and the left child is always smaller than the parent, the right child is always greater than the parent Implementation ...

My First Commit

I am a student from University of Nottingham. I am not a Blogger. I didn't used to write anything in my spare time. But now i am going to change. I want to log what i learned in and extra-curriculum. As an amateur programmer, it is a platform for me and others to discuss, recap and discover. First, i want to summarise what i learned about Data Structure and Algorithm, which will grab something from "G52ACE" course and other resources. I'd try my best to update everyday and by Christmas i might write something about Operating System and Git. After the exam in Jan, i will started to write AI things and Android Development. Let's get started!