《演算法》(1)--動態串連與演算法分析__演算法

來源:互聯網
上載者:User
First Chapter of <Algorithms>(a)--Dynamic Connectivity and Algorithm Analysis
Basic Statement

This poster of algorithms is based on the book and the lecture of algorithms on coursera which is taught by Robert Sedgewick and Kevin Wayne, writers of the book mentioned above.

First chapter of “Algorithm” is the fundamental of java programming language and a brief introduction to algorithms and data structure, including their concepts. some interesting problems and the basic implementations of them. Introduction Why we study algorithms Their impact is broad and far-reaching. Old roots, new opportunities. To solve problems that could not otherwise be addressed. For intellectual stimulation. To become a proficient programmer. They may unlock the secrets of life and of the universe. For fun and profit.

After summarizing some reason why we study algorithms, we may draw a conclusion that algorithms are widely used and we can get much fun from it. Steps to developing a usable algorithm Model the problem Find an algorithm to solve it Fast enough? Fits in memory? If not, figure out why Find a way to address the problem Iterate until satisfied

Developing a usable algorithm is the same as mathematical modeling which using iteration until the performance is satisfied. Scientific method:

Develop hypotheses about performance, create mathematical models. and run experiments to test them, repeating the process as necessary. Algorithm

The term algorithm is used in computer science to describe a finite, deterministic, and effective problem-solving method suitable for implementation as a computer program. Dynamic Connectivity-Union Find Form of Union Find problem

Given a set of N objects.
- Union command: connect two objects
- Find/connected query: is there a path connecting the two objects? QuickFind

Implementation of Quick-Find QuickFind.java

/** * Created by WilliamYi * 3/11/2017 * the direct implementation of Union-Find * the connected two points have the same index */package quickfind;public class QuickFind {    private int[] id;    //set id of each object to itself    public void QuickFindUF (int N) {        id = new int[N];            for(int i = 0; i < N; i++) id[i] = i;    }    //check whether p and q are connected    public boolean connected(int p, int q) {        return id[p] == id[q];    }    //change all entries with id[p] to id[q]    public void union(int p, int q) {        int pid = id[p];        int qid = id[q];        for(int i = 0; i < id.length; i++) {            if(id[i] == pid) id[i] = qid;        }    }}
QuickFindTest.java
package quickfind;public class QuickFindTest {    public static void main(String [] args) {        QuickFind qf = new QuickFind();        qf.QuickFindUF(6);        qf.union(0, 3);        qf.union(1, 4);        qf.union(4, 5);        qf.union(2, 5);        System.out.println(qf.connected(1, 3));        System.out.println(qf.connected(1, 2));    }}
QuickUnion

Implementation of QuickUnion QuickUnion.java

/** * Created by WilliamYi * 3/11/2017 * the quick union implementation of Union-Find */package quickunion;public class QuickUnion {    private int[] id;    public void QuickUnionUF(int N) {        id = new int[N];        for(int i = 0; i < N; i++) id[i] = i;    }    public int root(int i) {        while(i != id[i]) i = id[i];        return i;    }    public boolean connected(int p, int q) {        return root(p) == root(q);    }    public void union(int p, int q) {        int i = root(p);        int j = root(q);        id[i] = j;    }}
QuickUnionTest.java
package quickunion;public class QuickUnionTest {    public static void main(String[] args) {        QuickUnion qu = new QuickUnion();        qu.QuickUnionUF(6);        qu.union(0, 3);        qu.union(1, 4);        qu.union(4, 5);        qu.union(2, 5);        System.out.println(qu.connected(1, 3));        System.out.println(qu.connected(1, 2));    }}
Weighted Quick Union

The main idea of weighted quick union is to create a balance treee.

Implementation of WeightedQuickUnion WeightedQuickUnion.java

package weightedquickunion;public class WeightedQuickUnion {    private int[] id;    private int[] sz;    public void WeightedQU(int N) {        id = new int[N];        sz = new int[N];        for(int i = 0; i < N; i++) {            id[i] = i;            sz[i] = 1;        }    }    public int root(int i) {        while(i != id[i]) i = id[i];        return i;    }    public void union(int p, int q) {        int i = root(p);        int j = root(q);        if(i == j) return;        if(sz[i] < sz[j]) {id[i] = j; sz[j] += sz[i];}        else    {id[j] = i; sz[i] += sz[j];}    }    public boolean connected(int p, int q) {        return root(p) == root(q);    }}
WeightedQUTest.java
package weightedquickunion;public class WeightedQUTest {    public static void main(String[] args) {        WeightedQuickUnion wqu = new WeightedQuickUnion();        wqu.WeightedQU(6);        wqu.union(0, 3);        wqu.union(1, 4);        wqu.union(4, 5);        wqu.union(2, 5);        System.out.println(wqu.connected(1, 3));        System.out.println(wqu.connected(1, 2));//      wqu.WeightedQU(6);//      wqu.union(0, 1);//      System.out.println(wqu.connected(1, 0));    }}
Analysis of algorithm Scientific method applied to analysis of algorithms Scientific Method Observe some feature of the natural world Hypothesize a model that is consistent with the observations Predict events using the hypothesis Verify the predictions by making further observations Validate by repeating until the hypothesis and observations agree Principles Experiments must be reporducible Hypotheses must be falsifiable Three-Sum Description

Given N distinct integers, how many triples sum to exactly zero? BruteForceMethod BruteForceMethod.java

package threesum;public class BruteForceMethod {    public int NumOfThreeSum(int[] a) {        int count = 0;        for(int i = 0; i < a.length; i++)            for(int j = i+1; j < a.length; j++)                for(int k = j+1; k < a.length; k++)                    if(a[i] + a[j] + a[k] == 0) {                        System.out.println(a[i] + " " + a[j] + " " + " " + a[k]);                        count++;                    }           return count;    }}
ThreeSumTest.java
package threesum;public class ThreeSumTest {    public static void main(String[] args) {        BruteForceMethod bfm = new BruteForceMethod();        int a[] = {10, 20, 0, -10, 40, -40, 30};        System.out.println(bfm.NumOfThreeSum(a));    }}
Time Calculator and Random Size Three Sum Stopwatch.java
package threesum;public class Stopwatch {    private final long start;    public Stopwatch() {        start = System.currentTimeMillis();    }    public double elapsedTime() {        long now = System.currentTimeMillis();        return (now - start) / 1000.0;    }}
BruteForceMethod.java
package threesum;public class BruteForceMethod {    public int NumOfThreeSum(int[] a) {        int count = 0;        for(int i = 0; i < a.length; i++)            for(int j = i+1; j < a.length; j++)                for(int k = j+1; k < a.length; k++)                    if(a[i] + a[j] + a[k] == 0) {                        System.out.println(a[i] + " " + a[j] + " " + " " + a[k]);                        count++;                    }           return count;    }}
ThreeSumTest.java
package threesum;import java.util.*;public class ThreeSumTest {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        System.out.print("Please input the size: ");        int N = sc.nextInt();        int a[] = new int[N];        BruteForceMethod bfm = new BruteForceMethod();        Stopwatch timer = new Stopwatch();        //int a[] = {10, 20, 0, -10, 40, -40, 30};        for(int i = 0; i < N; i++) {            //generate random number ranging from -10000 to 10000            a[i] = (int)(Math.random() * 10000 * Math.pow(-1, (int)(10 * Math.random())));        }        System.out.println(bfm.NumOfThreeSum(a));        double time = timer.elapsedTime();        System.out.println("Running time is" + time + "seconds");    }}
Binary Search BinarySearch.java
package binarysearch;public class BinarySearch {    public int rank(int key, int[] a) {        int lo = 0;        int hi = a.length - 1;        //lo represent low, hi represent high        while (lo <= hi) {            int mid = lo + (hi - lo) / 2;            //use this form instead of (hi + lo)/2 because it can prevent overflow            if(key < a[mid]) hi = mid - 1;            else if(key > a[mid]) lo = mid + 1;            else return mid;        }        return -1;    }}
BinarySearchTest.java
package binarysearch;import java.util.Arrays;import java.util.Scanner;public class BinarySearchTest {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        BinarySearch bs = new BinarySearch();        int a[] = {11,23,54,68,15,18,19,56,35};        Arrays.sort(a);        System.out.println("The sorted array is: ");        for(int i = 0; i < a.length; i++) {            System.out.print(a[i] + "  ");        }        System.out.println();        System.out.print("Please input the searching number: ");        int key = sc.nextInt();        System.out.print("The index of the it is: " + bs.rank(key, a));    }}
An N2logN algorithm for Three-Sum


SortingBase3Sum.java

package sortingbased3sum;import java.util.Arrays;public class SortingBased3Sum {    public int SortingBsedThreeSum(int[] a) {        int count = 0;        Arrays.sort(a);        for(int i = 0; i < a.length; i++) {            for(int j = 1 + 1; j < a.length; j++) {                if(Arrays.binarySearch(a, -(a[i]+a[j])) >= 0) {                         System.out.println(a[i] + " " + a[j] + " " + (-a[i]-a[j]));                        count++;                    }            }        }        return count;    }}
SortingBased3SumTest.java
package sortingbased3sum;import java.util.Scanner;public class SortingBased3SumTest {    public static void main(String [] args) {Scanner sc = new Scanner(System.in);        System.out.print("Please input the size: ");        int N = sc.nextInt();        int a[] = new int[N];        SortingBased3Sum sortingbased3sum = new SortingBased3Sum();        //int a[] = {10, 20, 0, -10, 40, -40, 30};        for(int i = 0; i < N; i++) {            //generate random number ranging from -10000 to 10000            a[i] = (int)(Math.random() * 10000 * Math.pow(-1, (int)(10 * Math.random())));        }        System.out.println(sortingbased3sum.SortingBsedThreeSum(a));    }}

END

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.