# 译码算法

## 寻找特定的声音序列

### viterbi_graph.h

``````#include <iostream>
#include <cmath>
#include <ctime>
#include "stdlib.h"
#include <string.h>
using namespace std;

#define MAXVEX 100

enum color{WHITE,GRAY,BLACK};
enum which_edge{NONE,TREE,BACK,FORWARD,CROSS};

typedef int status;
typedef string VertexType;
typedef int EdgeType;

typedef struct EdgeNode
{
int Edgestart;
int Edgeend;  //邻接点域，储存该顶点对应的下标
EdgeType weight;  //用于存储权值

int type;
struct EdgeNode *next;  //下一个邻接点
}EdgeNode;

typedef struct VertexNode  //顶点表结点
{
VertexType data;  //顶点域，存储顶点信息

int color;
int touch,finish;   //访问开始时间和结束时间
EdgeNode* FirstEdge;  //边表头指针
int parent; //指向遍历的父结点

typedef struct
{
int numNodes,numEdges;

{
EdgeNode *e;
cout<<"Input the number of vertexes and edges: "<<endl;
cin>>G->numNodes>>G->numEdges;

//输入顶点表的信息
for(int i=0;i<G->numNodes;i++)
{
cout<<"Input the data (information) of vertexes "<<i+1<<endl;

}

int beg,end;
for(int k=0;k<G->numEdges;k++)
{
cout<<"Input the serial number of edges (Vi,Vj) "<<endl;
cout<<"Input the vertex of beginning: ";
cin>>beg;

cout<<"Input the vertex of endding: ";
cin>>end;

e=(EdgeNode *)malloc(sizeof(EdgeNode));
e->Edgeend=end;
e->Edgestart=beg;
e->weight=0;
e->type=NONE;
}
}``````

### graph_algorithm.h

``````#include "viterbi_graph.h"

int path_time=0;
int path_exist=0;
int path_print_signal=0;

{
EdgeNode *cur_edge;
for(int i=0;i<G->numNodes;i++)
{
{
cout<<cur_edge->Edgeend<<"  ";
}
cout<<endl;
}
}

{
path_time++;

{
if(cur_e->Edgeend==end)
{
path_exist=1;
}
if(path_exist==1)
return;
int cur_end=cur_e->Edgeend;
{
DFS_visit(G,cur_end,end);
cur_e->type=TREE;
}
{
cur_e->type=BACK;
}
{
cur_e->type=FORWARD;
else
cur_e->type=CROSS;
}
}

path_time++;
}

{
for(int u=0;u<G->numNodes;u++)
{
}
path_time=0;
DFS_visit(G,start,end);
}

{
if(path_exist==1)
{
cout<<" "<<start;
{
if(e_ptr->Edgeend==end)
{
cout<<" "<<end;
path_print_signal=1;
return;
}
}
if(path_print_signal==0)
{
{
if(e_ptr->type==TREE)
path_print(G,e_ptr->Edgeend,end);
}
}
}
else
{
cout<<"The path is not exist"<<endl;
}
}``````

### viterbi_algorithm.cpp

``````#include "graph_algorithm.h"

int main()
{
CreateALGraph(&G);

print_graph(&G);

int start,end;
cout<<"Input the start of the edge : "<<endl;
cin>>start;
cout<<"Input the end of the edge : "<<endl;
cin>>end;
DFS(&G,start,end);
cout<<endl;
cout<<path_exist<<endl;

cout<<"The path is :"<<endl;
path_print(&G,start,end);

return 0;
}``````

## 随机游动

### 状态转移函数

``````answer=e->probability*DFS_compute(G,e->e_end,end);
{
}``````

### probability_graph.h

``````#include <iostream>
#include <string.h>
#include "stdlib.h"

using namespace std;

#define MAXVEX 100

typedef int status;
typedef string VertexType;
typedef double EdgeType;

typedef struct Edge
{
int e_start;
int e_end;  //邻接点域
EdgeType probability;  //用于存储权值，即边的概率

struct Edge *next;
}Edge;

typedef struct Vertex
{
VertexType data;

int direction;  //用于指明节点下一步该往哪里走
double res;  //用于储存最后的结果，初始化为-1
//direction和res要联动

typedef struct
{

int numNodes,numEdges;

{
Edge *e;
cout<<"Input the number of vertexes and edges: "<<endl;
cin>>G->numNodes>>G->numEdges;

//输入顶点表信息
for(int i=0;i<G->numNodes;i++)
{
cout<<"Input the data (information) of vertexes "<<i+1<<endl;
}

int beg,end;
double prob;

for(int k=0;k<G->numEdges;k++)
{
cout<<"Input the serial number of Edges (Vi,Vj) "<<endl;
cout<<"Input the vertex of beginning: ";
cin>>beg;

cout<<"Input the vertex of endding: ";
cin>>end;

cout<<"Input the probability of edge: ";
cin>>prob;

e=(Edge *)malloc(sizeof(Edge));
e->e_start=beg;
e->e_end=end;
e->probability=prob;

}
}``````

### viterbi_probability.h

``````#include "probability_graph.h"

double maxpath=0;
int path_exist=0;

int path_print_signal=0;
int has_founded[MAXVEX];

double max(double a,double b)
{
return a>b?a:b;
}

{
for(int i=0;i<G->numNodes;i++)
has_founded[i]=0;

maxpath=DFS_compute(G,beg,end);
return maxpath;
}

{
if(beg==end)
{
return 1;
}
{
if(has_founded[beg]==1)
{
}
if(has_founded[beg]==0)
{
if(e->e_end==end)
{
path_exist=1;
{
}
}

{
}
}
}

has_founded[beg]=1;
}

{
Edge *cur_edge;
for(int i=0;i<G->numNodes;i++)
{
{
cout<<cur_edge->e_end<<"  ";
}
cout<<endl;
}
}``````

### viterbi_probability.cpp

``````#include "viterbi_probability.h"

int main()
{
CreateGraph(&G);

print_graph(&G);

int start,end;
cout<<"Input the start of the edge : "<<endl;
cin>>start;
cout<<"Input the end of the edge : "<<endl;
cin>>end;

double result=DFSTraverse(&G,start,end);
cout<<endl;
cout<<path_exist<<endl;
cout<<result<<endl;

cout<<"The max path is : "<<endl;
cout<<start<<" ";
if(path_exist==1)
{
while(pathID!=end)
{
cout<<pathID<<" ";