「算法图解java」算法图解pdf百度网盘
本篇文章给大家谈谈算法图解java,以及算法图解pdf百度网盘对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
java数据结构书籍推荐
1. 入门级
针对刚入门的同学,建议不要急着去看那些经典书,像《算法导论》、《算法》这些比较经典、权威的书。虽然书很好,但看起来很费劲,如果看不完,效果会很不好。所以建议先看两本入门级的趣味书:
《大话数据结构》
《算法图解》
大话数据结构
将理论讲的很有趣,不枯燥。作者结合生活中的例子去对每个数据结构和算法进行讲解,让人通俗易懂。
算法图解
这是一本像小说一样有趣的算法入门书,书中有大量的图解,通俗易懂。
看完上面一本或两本入门级的书,你就会对数据结构和算法有个大概认识和学习。但这些入门级的书缺少细节、不够系统。所以想要深入的学习数据结构和算法,光看这两本书肯定是不够的。
2. 不同语言的教科书
国内外很多大学都是将《数据结构和算法分析》作为教科书。这本书非常系统、严谨、全面,难度适中,很适合对数据结构和算法有些了解,并且已经掌握了至少一门语言的同学学习。针对不同的语言,分别有:
《数据结构与算法分析:C语言描述》
《数据结构与算法分析:C++描述》
《数据结构与算法分析:java语言描述》
如果你不会C、C++、java,会Python或者JavaScript,可以看:
《数据结构与算法JavaScript描述》
《数据结构与算法:Python语言描述》
3. 面试书籍
现在很多大厂的面试都会考算法题,这里推荐几本面试算法书籍:
《剑指offer》
《编程珠玑》
《编程之美》
剑指offer
为面试算法量身定做的一本书。几乎包含了所有常见的、经典的面试题,如果能搞懂书里面的内容,一般公司的算法面试都应该没问题。
编程珠玑
这本书豆瓣评分有9分,评分很高。这本书最大的特色是讲了很多海量数据的处理技巧。其他算法书籍很少涉及海量数据。
编程之美
有些作者是微软工程师,算法题目较难,比较适合要面试Google、Facebook这样的公司的人去看。
4. 经典书籍
现在数据结构与算法最经典的书籍就是:
《算法导论》
《算法》
《计算机程序设计艺术》
这三本书非常经典,但都很厚,看起来比较费劲,估计很少有人能全部看完。但如果想更深入地学一遍数据结构和算法,还是建议去看看。
算法导论
章节安排不是循序渐进,里面有各种算法正确性、复杂度的证明、推导,对数学功底有一定要求,看起来有些费劲。
算法
偏重讲算法。内容不够全面,对数据结构方面的知识讲的不多,动态规划这么重要的知识点却没有讲。
计算机程序设计艺术
这本书包括很多卷,相比于其他书籍有更好的深度、广度、系统性和全面性。但如果你对数据结构和算法不是特别感兴趣,没有很好的数学、算法、计算机基础,很难把这本书读完、读懂。
5. 课外阅读
有些算法书籍也比较适合在平时悠闲的时候翻翻看看:
《算法帝国》
《数学之美》
《算法之美》
这些书都列举了大量的列子来解释说明,非常通俗易懂。
如何用Java实现遗传算法?
通过遗传算法走迷宫。虽然图1和图2均成功走出迷宫,但是图1比图2的路径长的多,且复杂,遗传算法可以计算出有多少种可能性,并选择其中最简洁的作为运算结果。
示例图1:
示例图2:
实现代码:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
/**
* 用遗传算法走迷宫
*
* @author Orisun
*
*/
public class GA {
int gene_len; // 基因长度
int chrom_len; // 染色体长度
int population; // 种群大小
double cross_ratio; // 交叉率
double muta_ratio; // 变异率
int iter_limit; // 最多进化的代数
Listboolean[] individuals; // 存储当代种群的染色体
Labyrinth labyrinth;
int width; //迷宫一行有多少个格子
int height; //迷宫有多少行
public class BI {
double fitness;
boolean[] indv;
public BI(double f, boolean[] ind) {
fitness = f;
indv = ind;
}
public double getFitness() {
return fitness;
}
public boolean[] getIndv() {
return indv;
}
}
ListBI best_individual; // 存储每一代中最优秀的个体
public GA(Labyrinth labyrinth) {
this.labyrinth=labyrinth;
this.width = labyrinth.map[0].length;
this.height = labyrinth.map.length;
chrom_len = 4 * (width+height);
gene_len = 2;
population = 20;
cross_ratio = 0.83;
muta_ratio = 0.002;
iter_limit = 300;
individuals = new ArrayListboolean[](population);
best_individual = new ArrayListBI(iter_limit);
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public double getCross_ratio() {
return cross_ratio;
}
public ListBI getBest_individual() {
return best_individual;
}
public Labyrinth getLabyrinth() {
return labyrinth;
}
public void setLabyrinth(Labyrinth labyrinth) {
this.labyrinth = labyrinth;
}
public void setChrom_len(int chrom_len) {
this.chrom_len = chrom_len;
}
public void setPopulation(int population) {
this.population = population;
}
public void setCross_ratio(double cross_ratio) {
this.cross_ratio = cross_ratio;
}
public void setMuta_ratio(double muta_ratio) {
this.muta_ratio = muta_ratio;
}
public void setIter_limit(int iter_limit) {
this.iter_limit = iter_limit;
}
// 初始化种群
public void initPopulation() {
Random r = new Random(System.currentTimeMillis());
for (int i = 0; i population; i++) {
int len = gene_len * chrom_len;
boolean[] ind = new boolean[len];
for (int j = 0; j len; j++)
ind[j] = r.nextBoolean();
individuals.add(ind);
}
}
// 交叉
public void cross(boolean[] arr1, boolean[] arr2) {
Random r = new Random(System.currentTimeMillis());
int length = arr1.length;
int slice = 0;
do {
slice = r.nextInt(length);
} while (slice == 0);
if (slice length / 2) {
for (int i = 0; i slice; i++) {
boolean tmp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = tmp;
}
} else {
for (int i = slice; i length; i++) {
boolean tmp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = tmp;
}
}
}
// 变异
public void mutation(boolean[] individual) {
int length = individual.length;
Random r = new Random(System.currentTimeMillis());
individual[r.nextInt(length)] ^= false;
}
// 轮盘法选择下一代,并返回当代最高的适应度值
public double selection() {
boolean[][] next_generation = new boolean[population][]; // 下一代
int length = gene_len * chrom_len;
for (int i = 0; i population; i++)
next_generation[i] = new boolean[length];
double[] cumulation = new double[population];
int best_index = 0;
double max_fitness = getFitness(individuals.get(best_index));
cumulation[0] = max_fitness;
for (int i = 1; i population; i++) {
double fit = getFitness(individuals.get(i));
cumulation[i] = cumulation[i - 1] + fit;
// 寻找当代的最优个体
if (fit max_fitness) {
best_index = i;
max_fitness = fit;
}
}
Random rand = new Random(System.currentTimeMillis());
for (int i = 0; i population; i++)
next_generation[i] = individuals.get(findByHalf(cumulation,
rand.nextDouble() * cumulation[population - 1]));
// 把当代的最优个体及其适应度放到best_individual中
BI bi = new BI(max_fitness, individuals.get(best_index));
// printPath(individuals.get(best_index));
//System.out.println(max_fitness);
best_individual.add(bi);
// 新一代作为当前代
for (int i = 0; i population; i++)
individuals.set(i, next_generation[i]);
return max_fitness;
}
// 折半查找
public int findByHalf(double[] arr, double find) {
if (find 0 || find == 0 || find arr[arr.length - 1])
return -1;
int min = 0;
int max = arr.length - 1;
int medium = min;
do {
if (medium == (min + max) / 2)
break;
medium = (min + max) / 2;
if (arr[medium] find)
min = medium;
else if (arr[medium] find)
max = medium;
else
return medium;
} while (min max);
return max;
}
// 计算适应度
public double getFitness(boolean[] individual) {
int length = individual.length;
// 记录当前的位置,入口点是(1,0)
int x = 1;
int y = 0;
// 根据染色体中基因的指导向前走
for (int i = 0; i length; i++) {
boolean b1 = individual[i];
boolean b2 = individual[++i];
// 00向左走
if (b1 == false b2 == false) {
if (x 0 labyrinth.map[y][x - 1] == true) {
x--;
}
}
// 01向右走
else if (b1 == false b2 == true) {
if (x + 1 width labyrinth.map[y][x + 1] == true) {
x++;
}
}
// 10向上走
else if (b1 == true b2 == false) {
if (y 0 labyrinth.map[y - 1][x] == true) {
y--;
}
}
// 11向下走
else if (b1 == true b2 == true) {
if (y + 1 height labyrinth.map[y + 1][x] == true) {
y++;
}
}
}
int n = Math.abs(x - labyrinth.x_end) + Math.abs(y -labyrinth.y_end) + 1;
// if(n==1)
// printPath(individual);
return 1.0 / n;
}
// 运行遗传算法
public boolean run() {
// 初始化种群
initPopulation();
Random rand = new Random(System.currentTimeMillis());
boolean success = false;
while (iter_limit-- 0) {
// 打乱种群的顺序
Collections.shuffle(individuals);
for (int i = 0; i population - 1; i += 2) {
// 交叉
if (rand.nextDouble() cross_ratio) {
cross(individuals.get(i), individuals.get(i + 1));
}
// 变异
if (rand.nextDouble() muta_ratio) {
mutation(individuals.get(i));
}
}
// 种群更替
if (selection() == 1) {
success = true;
break;
}
}
return success;
}
// public static void main(String[] args) {
// GA ga = new GA(8, 8);
// if (!ga.run()) {
// System.out.println("没有找到走出迷宫的路径.");
// } else {
// int gen = ga.best_individual.size();
// boolean[] individual = ga.best_individual.get(gen - 1).indv;
// System.out.println(ga.getPath(individual));
// }
// }
// 根据染色体打印走法
public String getPath(boolean[] individual) {
int length = individual.length;
int x = 1;
int y = 0;
LinkedListString stack=new LinkedListString();
for (int i = 0; i length; i++) {
boolean b1 = individual[i];
boolean b2 = individual[++i];
if (b1 == false b2 == false) {
if (x 0 labyrinth.map[y][x - 1] == true) {
x--;
if(!stack.isEmpty() stack.peek()=="右")
stack.poll();
else
stack.push("左");
}
} else if (b1 == false b2 == true) {
if (x + 1 width labyrinth.map[y][x + 1] == true) {
x++;
if(!stack.isEmpty() stack.peek()=="左")
stack.poll();
else
stack.push("右");
}
} else if (b1 == true b2 == false) {
if (y 0 labyrinth.map[y - 1][x] == true) {
y--;
if(!stack.isEmpty() stack.peek()=="下")
stack.poll();
else
stack.push("上");
}
} else if (b1 == true b2 == true) {
if (y + 1 height labyrinth.map[y + 1][x] == true) {
y++;
if(!stack.isEmpty() stack.peek()=="上")
stack.poll();
else
stack.push("下");
}
}
}
StringBuilder sb=new StringBuilder(length/4);
IteratorString iter=stack.descendingIterator();
while(iter.hasNext())
sb.append(iter.next());
return sb.toString();
}
}
java中递归算法是什么怎么算的?
一、递归算法基本思路:
Java递归算法是基于Java语言实现的递归算法。递归算法是一种直接或者间接调用自身函数或者方法的算法。递归算法实质是把问题分解成规模缩小的同类问题的子问题,然后递归调用方法表示问题的解。递归往往能给我们带来非常简洁非常直观的代码形式,从而使我们的编码大大简化,然而递归的思维确实跟我们的常规思维相逆的,通常都是从上而下的思维问题,而递归趋势从下往上的进行思维。
二、递归算法解决问题的特点:
【1】递归就是方法里调用自身。
【2】在使用递归策略时,必须有一个明确的递归结束条件,称为递归出口。
【3】递归算法代码显得很简洁,但递归算法解题的运行效率较低。所以不提倡用递归设计程序。
【4】在递归调用的过程中系统为每一层的返回点、局部量等开辟了栈来存储。递归次数过多容易造成栈溢出等,所以一般不提倡用递归算法设计程序。
【5】在做递归算法的时候,一定把握出口,也就是做递归算法必须要有一个明确的递归结束条件。这一点是非常重要的。其实这个出口就是一个条件,当满足了这个条件的时候我们就不再递归了。
三、代码示例:
public class Factorial {
//this is a recursive function
int fact(int n){
if (n==1) return 1;
return fact(n-1)*n;
}
}
public class TestFactorial {
public static void main(String[] args) {
// TODO Auto-generated method stub
Factorial factorial=new Factorial();
System.out.println("factorial(5)="+factorial.fact(5));
}
}
代码执行流程图如下:
此程序中n=5就是程序的出口。
算法图解java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于算法图解pdf百度网盘、算法图解java的信息别忘了在本站进行查找喔。