A. 如何學習數據結構與演算法
1、記住數據結構,記住演算法思想(是什麼)記住數據結構最直觀的東西;記憶該數據結構的定義、性質、特點等。很多東西的理解和創新都是以記憶為前提的。
2、進行大量相關編程練習,用編程語言去實現某一數據結構上的演算法(怎麼辦)
很多時候,理解一個演算法很容易,很容易在紙上去模擬一個演算法的實現過程。但具體實現,則是另一回事。一定得先自己思考,然後再去看書中給的編程語言實現。
3、“記住”特定情景下,利用某一特定的數據結構,去解決問題 (為什麼+怎麼辦)
每介紹一種數據結構,浙大數據結構與演算法的MOOC課程都會有一個實際問題來作為“引子”,回答了“這種數據結構為什麼會出現”。有的是為了實現特定的操作,有的是為了時間和空間上(大部分考慮的是時間復雜性)效率的更高(所以,沒事的時候,分析一下演算法的時間復雜性)。這些東西,我們也須理解記憶。每一數據結構都有其特性,去解決某一類問題,我們需要去記憶,去感悟。
4、形成一個屬於自己的知識體系
如何去“記住”(記好筆記,多多復習);在學習過程中,遇到挫折,產生挫敗感該如何處理(這個是必然會發生的,總有難以理解不會的地方);如何進行心態方面的調整(欲速則不達,不過也有”敏捷學習“的概念)。
B. 數據結構中串模擬匹配中的KMP演算法能用簡單通俗的話解釋一下嗎謝謝啦!
錯位移動模式串,找出失配位置之前
能與模式串以最大長度配對的串的一部分
例子:模式串
a
b
c
a
b
d,
(d處失配,錯位移動模式串)
a
b
c
a
b
d
本例中模式串以最大長度配對的串的一部分為a
b
C. 數據結構與演算法題需要回答
《數據結構與演算法》模擬題
一、填空題:(共15分)(每空一分)
按照排序時,存放數據的設備,排序可分為<1> 排序和<2> 排序。內部排序和外部排序
圖的常用的兩種存儲結構是<3> 和<4> 。鄰接矩陣和鄰接表
數據結構中的三種基本的結構形式是<5> 線性結構 和<6> 樹型結構 、圖型結構<7> 。
一個高度為6的二元樹,最多有<8> 63 個結點。
線性查找的時間復雜度為:<9> O(n^2) ,折半查找的時間復雜度為:<10> O(nlogn) 、堆分類的時間復雜度為:<11> O(nlogn) 。
在採用散列法進行查找時,為了減少沖突的機會,散列函數必須具有較好的隨機性,在我們介紹的幾種散列函數構造法中,隨機性最好的是<12> 隨機數 法、最簡單的構造方法是除留余數法<13> 。
線性表的三種存儲結構是:數組、<14> 鏈表 、<15> 靜態鏈表 。
二、回答下列問題:(共30分)
現有如右圖的樹,回答如下問題:看不見圖
根結點有:
葉結點有:
具有最大度的結點:
結點的祖先是:
結點的後代是:
棧存放在數組A[m]中,棧底位置是m-1。試問:
棧空的條件是什麼?top=m-1
棧滿的條件是什麼?top=-1
數據結構和抽象數據型的區別與聯系:
數據結構(data structure)—是相互之間存在一種或多種特定關系的數據元素的集合。數據元素相互之間的關系稱為結構。
抽象數據類型(ADT):是指一個數學模型(數據結構)以及定義在該模型(數據結構)上的一組操作。
D. 數據結構經典演算法有哪些
二叉樹遍歷:
status initqueue(Queue &Q)
{//初始化一個空隊列
Q.base=(QElemtype *)malloc(MAXSIZE*sizeof(QElemtype));
if(!Q.base)
exit(OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
status inqueue(Queue &Q,BiTree e)
{//將元素e入隊
if((Q.rear+1)%MAXSIZE==Q.front)
return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXSIZE;
return OK;
}
status outqueue(Queue &Q,BiTree &e)
{//刪除隊頭元素,並用e返回其值
if(Q.front==Q.rear)
return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAXSIZE;
return OK;
}
status emptyqueue(Queue Q)
{//若隊列空,返回TRUE,否則返回FALSE
if(Q.front==Q.rear)
return TRUE;
return FALSE;
}
//以下是二叉樹的演算法
void creattree(BiTree &t)
{//先序順序建立二叉樹t
char ch;
ch=getchar();
if(ch==' ')
{
t=NULL;
return;
}
t=(BiTree)malloc(sizeof(BiNode));
if(!t) exit(OVERFLOW);
t->data=ch;
creattree(t->lchild);
creattree(t->rchild);
}
void print(TElemtype e)
{
printf("%c",e);
}
void pretraverse(BiTree t, void (*visit)(TElemtype e))
{//先序遍歷二叉樹t
if(t)
{
(*visit)(t->data);
pretraverse(t->lchild,visit);
pretraverse(t->rchild,visit);
}
}
void intraverse(BiTree t, void (*visit)(TElemtype e))
{//中序遍歷二叉樹t
if(t)
{
intraverse(t->lchild,visit);
(*visit)(t->data);
intraverse(t->rchild,visit);
}
}
void posttraverse(BiTree t, void (*visit)(TElemtype e))
{//後序遍歷二叉樹t
if(t)
{
posttraverse(t->lchild,visit);
posttraverse(t->rchild,visit);
(*visit)(t->data);
}
}
void leveltraverse(BiTree t, void (*visit)(TElemtype e))
{//層次遍歷二叉樹t
BiNode *p;
Queue Q;
//if(!t) return;
initqueue(Q);
p=t;
inqueue(Q,p);
while(!emptyqueue(Q))
{
outqueue(Q,p);
if(p)
{
(*visit)(p->data);
inqueue(Q,p->lchild);
inqueue(Q,p->rchild);
}
}
}
void destroytree(BiTree &t)
{
if(t==NULL) return;
else if(t->lchild==NULL&&t->rchild==NULL)
{
free(t);
return;
}
else{
destroytree(t->lchild);
destroytree(t->rchild);
free(t);
return;
}
}
E. PYTHON的數據結構和演算法介紹
當你聽到數據結構時,你會想到什麼?
數據結構是根據類型組織和分組數據的容器。它們基於可變性和順序而不同。可變性是指創建後改變對象的能力。我們有兩種類型的數據結構,內置數據結構和用戶定義的數據結構。
什麼是數據演算法-是由計算機執行的一系列步驟,接受輸入並將其轉換為目標輸出。
列表是用方括弧定義的,包含用逗號分隔的數據。該列表是可變的和有序的。它可以包含不同數據類型的混合。
months=['january','february','march','april','may','june','july','august','september','october','november','december']
print(months[0])#print the element with index 0
print(months[0:7])#all the elements from index 0 to 6
months[0]='birthday #exchange the value in index 0 with the word birthday
print(months)
元組是另一種容器。它是不可變有序元素序列的數據類型。不可變的,因為你不能從元組中添加和刪除元素,或者就地排序。
length, width, height =9,3,1 #We can assign multiple variables in one shot
print("The dimensions are {} * {} * {}".format(length, width, height))
一組
集合是唯一元素的可變且無序的集合。它可以讓我們快速地從列表中刪除重復項。
numbers=[1,2,3,4,6,3,3]
unique_nums = set(numbers)
print(unique_nums)
models ={'declan','gift','jabali','viola','kinya','nick',betty' }
print('davis' in models)#check if there is turner in the set models
models.add('davis')
print(model.pop())remove the last item#
字典
字典是可變和無序的數據結構。它允許存儲一對項目(即鍵和值)
下面的例子顯示了將容器包含到其他容器中來創建復合數據結構的可能性。
* 用戶定義的數據結構*
使用數組的堆棧堆棧是一種線性數據結構,其中元素按順序排列。它遵循L.I.F.O的機制,意思是後進先出。因此,最後插入的元素將作為第一個元素被刪除。這些操作是:
溢出情況——當我們試圖在一個已經有最大元素的堆棧中再放一個元素時,就會出現這種情況。
下溢情況——當我們試圖從一個空堆棧中刪除一個元素時,就會出現這種情況。
隊列是一種線性數據結構,其中的元素按順序排列。它遵循先進先出的F.I.F.O機制。
描述隊列特徵的方面
兩端:
前端-指向起始元素。
指向最後一個元素。
有兩種操作:
樹用於定義層次結構。它從根節點開始,再往下,最後的節點稱為子節點。
鏈表
它是具有一系列連接節點的線性數據。每個節點存儲數據並顯示到下一個節點的路由。它們用來實現撤銷功能和動態內存分配。
圖表
這是一種數據結構,它收集了具有連接到其他節點的數據的節點。
它包括:
演算法
在演算法方面,我不會講得太深,只是陳述方法和類型:
原文:https://www.tuicool.com/articles/hit/VRRvYr3
F. 遺傳演算法的模擬 數據結構題目
我這里給出了一個簡單的模板如果需要編代碼填代碼的地方已經有提示了
/*package otherFile;
import java.util.Random;
import tGraph.TdcppGraph;
import shuffP.*;
*/
/**************
*
* @author vaqeteart
* 這里是遺傳演算法的核心框架遺傳演算法的步驟:
* 遺傳演算法核心部分的演算法描述
* 演算法步驟:
* 1、初始化
* 1.1、生成初始種群編碼
* 1.2、計算每個個體的適配值。
* 1.3、記錄當前最優適配值和最優個體
* 2、選擇和遺傳,
* 2.0、若當前最優適配值多次小於已有的最優適配值(或相差不大)很多次,或者進化的次數超過設定的限制,轉4。
* 2.1、按照與每個個體的適配值成正比的概率選擇個體並復制,復制之後個體的數目和原始種群數目一樣。
* 2.2、(最好先打亂復制後種群的個體次序)對復制後個體進行兩兩配對交叉,生成相同數目的的下一代種群。
* 2.3、對下一代種群按照一定的概率進行變異
* 2.4、計算每個個體的適配值。
* 2.5、記錄當前最優適配值和最優個體
* 2.6、轉2
* 3、返回當前最優適配值以及其對應的編碼,結束。
*
* 注意:
* 1.這里的內容相當於一個模板,編寫具體的遺傳演算法的時候,可以按照這個模板的形式編寫。
* 2.應該填寫代碼的地方都有提示的標記。
*/
public class GAKernel
{
//number of population
int popNum;//set the number to 20 in constructor
//current evolution times
int evolutionTim;
//limit of the evolution times
int evolutionLim;//set the number to 20 in constructor
//unaccepted times
//int eliminTim;
//limit of unaccepted times
//int eliminLim;
//current best euler code
//int curBestCode[];
//current best fitness
int curBestFitness;
//fitness of every indivial
int iFitness[];
//fator of compute the fitness
int factor;
//..................other members.............................................
//the graph
//public TdcppGraph tpGraph;
//the eula code group
//int codes[][];
//every population
//
//constructor
GAKernel(TdcppGraph tG,int eulerCode[])
{
popNum = 32;//2*2*2*2*2
//factor = Integer.MAX_VALUE / popNum;//to avoid overflow when select,for every fitness
evolutionTim = 0;/////
evolutionLim = 15;///////
//this.tpGraph=new TdcppGraph(tG);
//eliminTim = 0;
//eliminLim
curBestFitness = 0;
//curBestCode = new int[eulerCode.length];
//for(int i = 0; i < curBestCode.length; ++i)
//{
// curBestCode[i] = eulerCode[i];
//}
//??curBestFitness
iFitness = new int[popNum];
//codes = new int[popNum][];//lines
for(int i = 0; i < popNum; ++i)
{
//codes[i] = new int[eulerCode.length];
iFitness[i] = 0;
}
System.out.println("構造函數,需要填入代碼");
}
//initialize the originalpopulation
void initPopulation()
{
//.......................初始化種群........................................
//int tmpCode[] = new int[curBestCode.length];
//get the initial indivial
//for(int i = 0; i < curBestCode.length; ++i)
//{
// tmpCode[i] = curBestCode[i];
// codes[0][i] = tmpCode[i];
//}
//ShuffEP s = new ShuffEP(this.tpGraph);
//for(int i = 1; i < popNum; ++i)
//{
// s.shuff(tmpCode);
// for(int j = 0; j < tmpCode.length; ++j)
// {
// codes[i][j] = tmpCode[j];
// }
//}
System.out.println("初始化種群,需要填入代碼");
//get the initial fitness to the member iFitness
computeFitness();
//get the initial best indivial and fitness
recordBest();
}
//compute the fitness of every indivial in current population
void computeFitness()
{
//........................計算每個個體適應度.......................
//int time = 0;
//for(int i = 0; i < popNum; ++i)
//{
// time = 0;
// for(int j = 0; j < codes[i].length - 1; ++j)
// {
// time += tpGraph.Edge(codes[i][j], codes[i][j + 1]).getCost(time);
// }
// iFitness[i] = factor - time;
// if(iFitness[i] < 0)
// {
// System.out.println("錯誤,某個個體適應度過小使得適配值出現負數");//lkdebug
// System.exit(1);
// }
//}
System.out.println("計算每個個體適應度,需要填入代碼");
}
//record the current best fitness and the according indivial
void recordBest()
{
int bestIndex = -1;
for(int i = 0; i < popNum; ++i)
{
if(curBestFitness < iFitness[i])
{
curBestFitness = iFitness[i];
bestIndex = i;
}
}
//............................記錄最優個體.............................
if(bestIndex > -1)
{
// for(int i = 0; i < curBestCode.length; ++i)
// {
// curBestCode[i] = codes[bestIndex][i];
// }
}
System.out.println("記錄最優個體,需要填入代碼");
}
//selection and reproce indivial in population
void selIndivial()
{
int tmpiFitness[] = new int[iFitness.length];
tmpiFitness[0] = iFitness[0];
//建立臨時群體用於選擇交換
//.................................復制個體...............................
//清除原來的群體
//int tmpCode[][] = new int[popNum][];
//for(int i = 0; i < codes.length; ++i)
//{
// tmpCode[i] = new int[codes[i].length];//???
// for(int j = 0; j < codes[i].length; ++j)
// {// to tmpCode and reset codes
// tmpCode[i][j] = codes[i][j];
// codes[i][j] = -1;
// }
//}
System.out.println("復制個體,需要填入代碼");
for(int i = 1; i < tmpiFitness.length; ++i)
{
tmpiFitness[i] = tmpiFitness[i - 1] + iFitness[i];
//iFitness[i] = 0;
}
//輪盤賭選擇個體
for(int i = 0; i < popNum; ++i)
{
int rFit = new Random().nextInt(tmpiFitness[tmpiFitness.length - 1]);
for(int j = 0; j < tmpiFitness.length; ++j)
{
if(rFit < tmpiFitness[j])
{
rFit = j;//record the index of the indivial
break;
}
}
if(rFit == 0)
{
iFitness[i] = tmpiFitness[rFit];
}
else
{
iFitness[i] = tmpiFitness[rFit] - tmpiFitness[rFit - 1];// fitness
}
//....................................選擇個體...........................
//for(int j = 0; j < tmpCode[rFit].length; ++j)
//{
// codes[i][j] =tmpCode[rFit][j];
//}
System.out.println("選擇個體,需要填入代碼");
}
//get the copied fitness in iFitness
}
//match every two indivial and cross the code
void matchCross()
{
//........................需要填入代碼................................
System.out.println("配對交叉,需要填入代碼");
}
//mutate by a specifical probability
void mutate()
{
//........................按照一定的概率進行變異.......................
System.out.println("按照一定的概率進行變異,需要填入代碼");
}
//evolve current population
void evolve()
{
selIndivial();
matchCross();
mutate();
}
//compute the approximative best value by GA
//find approximative best solution by GA
public void compute()
{
initPopulation();
//while((evolutionTim < evolutionLim) && (eliminTim < eliminLim))
while(evolutionTim < evolutionLim)
{
evolve();
//get the initial fitness to the member iFitness
computeFitness();
//get the initial best indivial and fitness
recordBest();
++evolutionTim;
}
}
}