給定n個整數a , a , ,an 1 2 組成的序列。序列中元素i a 的符號定義為:
ï î
ï í
ì
- <
=
>
=
1 0
0 0
1 0
sgn( )
i
i
i
i
a
a
a
a
符號平衡問題要求給定序列的最長符號平衡段的長度L,即:
þ ý ü
î í ì
= + - = å
=
£ £ £
max 1| sgn( ) 0
1
j
k i
i j n k
L j i a 。
例如,當n=10,相應序列為:1,1,-1,-2,0,1,3,-1,2,-1 時,L=9。
Floyd-Warshall算法描述
1)適用范圍:
a)APSP(All Pairs Shortest Paths)
b)稠密圖效果最佳
c)邊權可正可負
2)算法描述:
a)初始化:dis[u,v]=w[u,v]
b)For k:=1 to n
For i:=1 to n
For j:=1 to n
If dis[i,j]>dis[i,k]+dis[k,j] Then
Dis[I,j]:=dis[I,k]+dis[k,j]
c)算法結束:dis即為所有點對的最短路徑矩陣
3)算法小結:此算法簡單有效,由于三重循環結構緊湊,對于稠密圖,效率要高于執行|V|次Dijkstra算法。時間復雜度O(n^3)。
考慮下列變形:如(I,j)∈E則dis[I,j]初始為1,else初始為0,這樣的Floyd算法最后的最短路徑矩陣即成為一個判斷I,j是否有通路的矩陣。更簡單的,我們可以把dis設成boolean類型,則每次可以用“dis[I,j]:=dis[I,j]or(dis[I,k]and dis[k,j])”來代替算法描述中的藍色部分,可以更直觀地得到I,j的連通情況。
function [U,center,result,w,obj_fcn]= fenlei(data)
[data_n,in_n] = size(data)
m= 2 % Exponent for U
max_iter = 100 % Max. iteration
min_impro =1e-5 % Min. improvement
c=3
[center, U, obj_fcn] = fcm(data, c)
for i=1:max_iter
if F(U)>0.98
break
else
w_new=eye(in_n,in_n)
center1=sum(center)/c
a=center1(1)./center1
deta=center-center1(ones(c,1),:)
w=sqrt(sum(deta.^2)).*a
for j=1:in_n
w_new(j,j)=w(j)
end
data1=data*w_new
[center, U, obj_fcn] = fcm(data1, c)
center=center./w(ones(c,1),:)
obj_fcn=obj_fcn/sum(w.^2)
end
end
display(i)
result=zeros(1,data_n) U_=max(U)
for i=1:data_n
for j=1:c
if U(j,i)==U_(i)
result(i)=j continue
end
end
end
Instead of finding the longest common
subsequence, let us try to determine the
length of the LCS.
Then tracking back to find the LCS.
Consider a1a2…am and b1b2…bn.
Case 1: am=bn. The LCS must contain am,
we have to find the LCS of a1a2…am-1 and
b1b2…bn-1.
Case 2: am≠bn. Wehave to find the LCS of
a1a2…am-1 and b1b2…bn, and a1a2…am and
b b b
b1b2…bn-1
Let A = a1 a2 … am and B = b1 b2 … bn
Let Li j denote the length of the longest i,g g
common subsequence of a1 a2 … ai and b1 b2
… bj.
Li,j = Li-1,j-1 + 1 if ai=bj
max{ L L } a≠b i-1,j, i,j-1 if ai≠j
L0,0 = L0,j = Li,0 = 0 for 1≤i≤m, 1≤j≤n.