《算法》(1)--动态连接与算法分析

本文介绍算法的基本概念及其重要性,并详细探讨动态连通性问题,包括快速查找、快速合并及加权快速合并等算法实现。此外,还分析了三数之和问题的不同解决方法。
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

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

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.

WeightedQuickUnion

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");
    }
}

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

Sorting Based Algorithm
Analysis

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

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值