代碼實(shí)現(xiàn)(Java)
成都創(chuàng)新互聯(lián)公司成都網(wǎng)站建設(shè)按需網(wǎng)站建設(shè),是成都網(wǎng)站建設(shè)公司,為地磅秤提供網(wǎng)站建設(shè)服務(wù),有成熟的網(wǎng)站定制合作流程,提供網(wǎng)站定制設(shè)計(jì)服務(wù):原型圖制作、網(wǎng)站創(chuàng)意設(shè)計(jì)、前端HTML5制作、后臺程序開發(fā)等。成都網(wǎng)站改版熱線:18982081108
1. 輸入
(1) 代表地圖二值二維數(shù)組(0表示可通路,1表示路障)
int[][] maps = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }
};123456789123456789
(2) 按照二維數(shù)組的特點(diǎn),坐標(biāo)原點(diǎn)在左上角,所以y是高,x是寬,y向下遞增,x向右遞增,我們將x和y封裝成一個(gè)類,好傳參,重寫equals方法比較坐標(biāo)(x,y)是不是同一個(gè)。
public class Coord
{
public int x;
public int y;
public Coord(int x, int y)
{
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj)
{
if (obj == null) return false;
if (obj instanceof Coord)
{
Coord c = (Coord) obj;
return x == c.x y == c.y;
}
return false;
}
}12345678910111213141516171819202122231234567891011121314151617181920212223
(3) 封裝路徑結(jié)點(diǎn)類,字段包括:坐標(biāo)、G值、F值、父結(jié)點(diǎn),實(shí)現(xiàn)Comparable接口,方便優(yōu)先隊(duì)列排序。
public class Node implements Comparable
{
public Coord coord; // 坐標(biāo)
public Node parent; // 父結(jié)點(diǎn)
public int G; // G:是個(gè)準(zhǔn)確的值,是起點(diǎn)到當(dāng)前結(jié)點(diǎn)的代價(jià)
public int H; // H:是個(gè)估值,當(dāng)前結(jié)點(diǎn)到目的結(jié)點(diǎn)的估計(jì)代價(jià)
public Node(int x, int y)
{
this.coord = new Coord(x, y);
}
public Node(Coord coord, Node parent, int g, int h)
{
this.coord = coord;
this.parent = parent;
G = g;
H = h;
}
@Override
public int compareTo(Node o)
{
if (o == null) return -1;
if (G + H o.G + o.H)
return 1;
else if (G + H o.G + o.H) return -1;
return 0;
}
}1234567891011121314151617181920212223242526272829303112345678910111213141516171819202122232425262728293031
(4) 最后一個(gè)數(shù)據(jù)結(jié)構(gòu)是A星算法輸入的所有數(shù)據(jù),封裝在一起,傳參方便。:grin:
public class MapInfo
{
public int[][] maps; // 二維數(shù)組的地圖
public int width; // 地圖的寬
public int hight; // 地圖的高
public Node start; // 起始結(jié)點(diǎn)
public Node end; // 最終結(jié)點(diǎn)
public MapInfo(int[][] maps, int width, int hight, Node start, Node end)
{
this.maps = maps;
this.width = width;
this.hight = hight;
this.start = start;
this.end = end;
}
}12345678910111213141516171234567891011121314151617
2. 處理
(1) 在算法里需要定義幾個(gè)常量來確定:二維數(shù)組中哪個(gè)值表示障礙物、二維數(shù)組中繪制路徑的代表值、計(jì)算G值需要的橫縱移動代價(jià)和斜移動代價(jià)。
public final static int BAR = 1; // 障礙值
public final static int PATH = 2; // 路徑
public final static int DIRECT_VALUE = 10; // 橫豎移動代價(jià)
public final static int OBLIQUE_VALUE = 14; // 斜移動代價(jià)12341234
(2) 定義兩個(gè)輔助表:Open表和Close表。Open表的使用是需要取最小值,在這里我們使用Java工具包中的優(yōu)先隊(duì)列PriorityQueue,Close只是用來保存結(jié)點(diǎn),沒其他特殊用途,就用ArrayList。
Queue openList = new PriorityQueue(); // 優(yōu)先隊(duì)列(升序)
List closeList = new ArrayList();1212
(3) 定義幾個(gè)布爾判斷方法:最終結(jié)點(diǎn)的判斷、結(jié)點(diǎn)能否加入open表的判斷、結(jié)點(diǎn)是否在Close表中的判斷。
/**
* 判斷結(jié)點(diǎn)是否是最終結(jié)點(diǎn)
*/
private boolean isEndNode(Coord end,Coord coord)
{
return coord != null end.equals(coord);
}
/**
* 判斷結(jié)點(diǎn)能否放入Open列表
*/
private boolean canAddNodeToOpen(MapInfo mapInfo,int x, int y)
{
// 是否在地圖中
if (x 0 || x = mapInfo.width || y 0 || y = mapInfo.hight) return false;
// 判斷是否是不可通過的結(jié)點(diǎn)
if (mapInfo.maps[y][x] == BAR) return false;
// 判斷結(jié)點(diǎn)是否存在close表
if (isCoordInClose(x, y)) return false;
return true;
}
/**
* 判斷坐標(biāo)是否在close表中
*/
private boolean isCoordInClose(Coord coord)
{
return coord!=nullisCoordInClose(coord.x, coord.y);
}
/**
* 判斷坐標(biāo)是否在close表中
*/
private boolean isCoordInClose(int x, int y)
{
if (closeList.isEmpty()) return false;
for (Node node : closeList)
{
if (node.coord.x == x node.coord.y == y)
{
return true;
}
}
return false;
}1234567891011121314151617181920212223242526272829303132333435363738394041424344454612345678910111213141516171819202122232425262728293031323334353637383940414243444546
(4) 計(jì)算H值,“曼哈頓” 法,坐標(biāo)分別取差值相加
private int calcH(Coord end,Coord coord)
{
return Math.abs(end.x - coord.x) + Math.abs(end.y - coord.y);
}12341234
(5) 從Open列表中查找結(jié)點(diǎn)
private Node findNodeInOpen(Coord coord)
{
if (coord == null || openList.isEmpty()) return null;
for (Node node : openList)
{
if (node.coord.equals(coord))
{
return node;
}
}
return null;
}123456789101112123456789101112
(6) 添加鄰結(jié)點(diǎn)到Open表
/**
* 添加所有鄰結(jié)點(diǎn)到open表
*/
private void addNeighborNodeInOpen(MapInfo mapInfo,Node current)
{
int x = current.coord.x;
int y = current.coord.y;
// 左
addNeighborNodeInOpen(mapInfo,current, x - 1, y, DIRECT_VALUE);
// 上
addNeighborNodeInOpen(mapInfo,current, x, y - 1, DIRECT_VALUE);
// 右
addNeighborNodeInOpen(mapInfo,current, x + 1, y, DIRECT_VALUE);
// 下
addNeighborNodeInOpen(mapInfo,current, x, y + 1, DIRECT_VALUE);
// 左上
addNeighborNodeInOpen(mapInfo,current, x - 1, y - 1, OBLIQUE_VALUE);
// 右上
addNeighborNodeInOpen(mapInfo,current, x + 1, y - 1, OBLIQUE_VALUE);
// 右下
addNeighborNodeInOpen(mapInfo,current, x + 1, y + 1, OBLIQUE_VALUE);
// 左下
addNeighborNodeInOpen(mapInfo,current, x - 1, y + 1, OBLIQUE_VALUE);
}
/**
* 添加一個(gè)鄰結(jié)點(diǎn)到open表
*/
private void addNeighborNodeInOpen(MapInfo mapInfo,Node current, int x, int y, int value)
{
if (canAddNodeToOpen(mapInfo,x, y))
{
Node end=mapInfo.end;
Coord coord = new Coord(x, y);
int G = current.G + value; // 計(jì)算鄰結(jié)點(diǎn)的G值
Node child = findNodeInOpen(coord);
if (child == null)
{
int H=calcH(end.coord,coord); // 計(jì)算H值
if(isEndNode(end.coord,coord))
{
child=end;
child.parent=current;
child.G=G;
child.H=H;
}
else
{
child = new Node(coord, current, G, H);
}
openList.add(child);
}
else if (child.G G)
{
child.G = G;
child.parent = current;
// 重新調(diào)整堆
openList.add(child);
}
}
}1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606112345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
(7) 回溯法繪制路徑
private void drawPath(int[][] maps, Node end)
{
if(end==null||maps==null) return;
System.out.println("總代價(jià):" + end.G);
while (end != null)
{
Coord c = end.coord;
maps[c.y][c.x] = PATH;
end = end.parent;
}
}12345678910111234567891011
(8) 開始算法,循環(huán)移動結(jié)點(diǎn)尋找路徑,設(shè)定循環(huán)結(jié)束條件,Open表為空或者最終結(jié)點(diǎn)在Close表
public void start(MapInfo mapInfo)
{
if(mapInfo==null) return;
// clean
openList.clear();
closeList.clear();
// 開始搜索
openList.add(mapInfo.start);
moveNodes(mapInfo);
}
/**
* 移動當(dāng)前結(jié)點(diǎn)
*/
private void moveNodes(MapInfo mapInfo)
{
while (!openList.isEmpty())
{
if (isCoordInClose(mapInfo.end.coord))
{
drawPath(mapInfo.maps, mapInfo.end);
break;
}
Node current = openList.poll();
closeList.add(current);
addNeighborNodeInOpen(mapInfo,current);
}
}
單元和區(qū)域和數(shù)值,,,中的最大
作者:何史提
鏈接:
來源:知乎
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
Apriori算法的理念其實(shí)很簡單,可是實(shí)現(xiàn)起上來卻復(fù)雜無比,因?yàn)楫?dāng)中無可避免用Set和Hash Table等高階的數(shù)據(jù)結(jié)構(gòu),而且有很多l(xiāng)oop用以讀取數(shù)據(jù)。
我不建議用Java,應(yīng)改用Python或Scala一類的語言。如果用Python,代碼大概50行左右,但可以想像用Java便看起來復(fù)雜得多??慈缦拢?/p>
from operator import and_
from itertools import combinations
class AprioriAssociationRule:
def __init__(self, inputfile):
self.transactions = []
self.itemSet = set([])
inf = open(inputfile, 'rb')
for line in inf.readlines():
elements = set(filter(lambda entry: len(entry)0, line.strip().split(',')))
if len(elements)0:
self.transactions.append(elements)
for element in elements:
self.itemSet.add(element)
inf.close()
self.toRetItems = {}
self.associationRules = []
def getSupport(self, itemcomb):
if type(itemcomb) != frozenset:
itemcomb = frozenset([itemcomb])
within_transaction = lambda transaction: reduce(and_, [(item in transaction) for item in itemcomb])
count = len(filter(within_transaction, self.transactions))
return float(count)/float(len(self.transactions))
def runApriori(self, minSupport=0.15, minConfidence=0.6):
itemCombSupports = filter(lambda freqpair: freqpair[1]=minSupport,
map(lambda item: (frozenset([item]), self.getSupport(item)), self.itemSet))
currentLset = set(map(lambda freqpair: freqpair[0], itemCombSupports))
k = 2
while len(currentLset)0:
currentCset = set([i.union(j) for i in currentLset for j in currentLset if len(i.union(j))==k])
currentItemCombSupports = filter(lambda freqpair: freqpair[1]=minSupport,
map(lambda item: (item, self.getSupport(item)), currentCset))
currentLset = set(map(lambda freqpair: freqpair[0], currentItemCombSupports))
itemCombSupports.extend(currentItemCombSupports)
k += 1
for key, supportVal in itemCombSupports:
self.toRetItems[key] = supportVal
self.calculateAssociationRules(minConfidence=minConfidence)
def calculateAssociationRules(self, minConfidence=0.6):
for key in self.toRetItems:
subsets = [frozenset(item) for k in range(1, len(key)) for item in combinations(key, k)]
for subset in subsets:
confidence = self.toRetItems[key] / self.toRetItems[subset]
if confidence minConfidence:
self.associationRules.append([subset, key-subset, confidence])
public class Paixu {
public static void main(String[] args) {
char[] in = "abcde".toCharArray();
new Paixu().paixu(in, in.length, 0);
}
private void paixu(char[] array, int n, int k) {
if (n == k) {
char[] out = new char[n];
for (int i = 0; i array.length; i++) {
out[i] = array[i];
}
System.out.println(new String(out));
} else {
for (int i = k; i n; i++) {
swap(array, k, i);
paixu(array, n, k + 1);
swap(array, i, k);
}
}
}
private void swap(char[] a, int x, int y) {
char temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
當(dāng)前文章:用java實(shí)現(xiàn)a算法代碼 java編程計(jì)算a+aa+aaa+aaan個(gè)a
分享路徑:http://muchs.cn/article0/doedpio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、移動網(wǎng)站建設(shè)、定制開發(fā)、網(wǎng)站收錄、靜態(tài)網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)