# PAT (Grade A) Spring 2019 Exam 7-3 Telefraud Detection (25 points) Phone Call Records.

2023-01-23   ES

1. The establishment of the picture

Figure is a data structure that expresses the relationship between “more pairs”.

It consists of non -empty limited vertices and limited edges collection.

1. The vertex collection is often represented by arrays.

A number of arrays indicates the vertex position.

The content of the array contains the vertex data, and it is necessary to add a logo scalar that has been judged whether it has been accessed to provide parameters for the remaining operations.

The data type definition is as follows:

struct Vertex {
T data;
bool isVisited;
}

2. Storage of the edge collection is commonly used with adjacent matrix or adjacent table.

side values (right values) indicate that there is a relationship between vertices, 0 indicates that there is no edge between the two vertices, that is, it doesn’t matter.

adjacent matrix is a two -dimensional array, Matrix [vertex] [vertex]. It can also be defined as one -dimensional array N (n+1)/2 in half of the province’s half of the space, which is not conducive to understanding and does not explain.

The simplest diagram is used to present the relationship between vertices. The vertex type can only contain only one logo variable used to determine whether it has been accessed. Use the Bool or INT array to represent the vertex.

<pre name="code" class="cpp">//graph.h

#ifndef GRAGH_H

#define GRAGH_H

class Graph {

public:

Graph(const int &capacity);

~Graph();

void insertEdge(const int &indexA, const int &indexB, const int &weight = 1);

// Member variables only need three.
private:

int capacity; // The number of apex of the figure.
Bool *pvertex; // The apex of the storage diagram.
int *pmatrix; // The edge of the storage diagram.
}

#endif

Implementation file:

//graph.cpp
#include "graph.h"

/*
Establish a picture through the initialization list.
And initialize the value of PVERTEX and PMATRIX.
Each elemental value of PVERTEX is initialized to FALSE, and each elemental value of PMATRIX is initialized to 0.
*/
inline
Graph :: Graph (const int & capacity):
CAPACITY (CAPACITY),
pvertex (New Bool [Capacity] ()),),),),),),),),),
pmatrix (new int [capacity * capacity] () {}

<span style = "color:#808000;"> inline </span> <pre style = "margin-top: 0px; margin-bottom: 0px; margin-heft: 0px; margin-right: 0px; -indent: 0; text-indent: 0px; "> <span style =" color:#800080; "> Graph </span> <span style =" color:#000000; ":: ~ </span> </span> span style = "color:#800080;"> Graph </span> <span style = "color:#000000;"> () </span> <span style = "color:#c0c0c0;"> </span> <span style = "color:#000000;"> {</span>
delete[] pVertex;
delete[] pMatrix;
}

inline

void
Graph
::insertEdge
(
const

int

&
indexA
,

const

int

&
indexB
,

const

int

weight
)

{

// Insert a side of point A to point B.
pMatrix[indexA * capacity + indexB] = weight;
// No direction map needs to insert a side from B to A.
pMatrix[indexB * capacity + indexA] = weight;
}

1. PVERTEX bidding indicates the vertex position.

2. PVERTEX value indicates whether the vertex has been accessed and initialized to false.

3. PMATRIX value represents the relationship between vertices, initialization to 0.

4. Add the edge information to form a picture.

2, the traversal of the picture

After the picture has the picture, we have to do some operations on it.

First of all traversal:

1. In -depth search for depth first search, which is similar to the preface of the tree.

2. Priority to search for Breadth First Search, similar to the layer sequence of trees.

//graph.h
public:
<span style="color:#808000;">    void</span><span style="color:#c0c0c0;"> </span><span style="color:#000000;">DFS</span><span style="color:#000000;">(</span><span style="color:#808000;">const</span><span style="color:#c0c0c0;"> </span><span style="color:#808000;">int</span><span style="color:#c0c0c0;"> </span><span style="color:#000000;">&</span>index<span style="color:#000000;">);</span><pre style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style="color:#c0c0c0;">    </span><span style="color:#808000;">void</span><span style="color:#c0c0c0;"> </span><span style="color:#000000;">BFS</span><span style="color:#000000;">(</span><span style="color:#808000;">const</span><span style="color:#c0c0c0;"> </span><span style="color:#808000;">int</span><span style="color:#c0c0c0;"> </span><span style="color:#000000;">&</span>index<span style="color:#000000;">);</span>
void resetVertex();

private:
const int& getEdgeWeight(const int &indexA, const int &indexB);

The resetvertex is used to reset all the vertices before the next traversal.

If it is represented by an INT array, it can be passed into a value to determine the access status of the vertex, and there is no need to resertex.

and the GETDGEIGHT function is the right value of the edge from point A to point B, which represents the relationship between the two vertices.

Its implementation is as follows:

//graph.cpp
#include <iostream>
#include <queue> // contains head files to provide queue containers for BFS.

<span style = "color:#808000;"> inline </span> <pre style = "margin-top: 0px; margin-bottom: 0px; margin-heft: 0px; margin-right: 0px; -indent: 0; text-indent: 0px; "> <span style =" color:#808000; "> const </span> <span style =" color:#c0c0c0; "> </span> <span style = "COLOR:#808000;"> int </span> <span style = "color:#000000;"> & </span> <span style = "color:#c0c0c0;"> </span> <span style = "COLOR:#800080;"> Graph </span> <span style = "color:#000000;"> :: </span> <span style = "color:#000000;"> GETEDGEIGHT </span> <span> <span> STYLE = "COLOR:#000000;"> (</span> <span style = "color:#808000;"> const </span> <span style = "color:#c0c0c0;"> </span> <span> style = "color:#808000;"> int </span> <span style = "color:#c0c0c0;"> </span> <span style = "color:#000000;"> & </span> <span STYLE = "COLOR:#000000;"> Indexa </span> <span style = "color:#000000;">, </span> <span style = "color:#c0c0c0;"> </span> <span> STYLE = "COLOR:#808000;"> CONST </span> <span style = "color:#c0c0c0;"> </span> <span style = "color:#808000;"> int </span> <span> style = "color:#c0c0c0;"> </span> <span style = "color:# 000000; "> & </span> <span style =" color:#000000; "> Indexb </span> <span style =" color:#000000; "> </span> <span style =" color: #c0c0c0; "> </span> <span style =" color:#000000; "> {</span>
return pMatrix[indexA * capacity + indexB];
}

// Print the index order of depth priority from INDEX.

void Graph::DFS(const int &index) {

std::cout << index << " ";
pVertex[index] = true;

for(int i(0); i < capacity; ++i) {

if(getEdgeWeight(index, i) > 0 && !pVertex[i])
DFS(i);
}
}

/Print the sequence of breadth priority from the start of index.
void Graph::BFS(const int &index) {

std::queue<int> que;
que.push(index);
pVertex[index] = true;

while(!que.empty()) {

int subcript(que.front());
que.pop();
std::cout << subcript << " ";

for(int i(0); i < capacity; ++i) {

if(getEdgeWeight(subcript, i) > 0 && !pVertex[i]) {

que.push(i);
pVertex[i] = true;
}
}
}
}

// reset the vertex is not accessed.

void Graph::resetVertex() {

for(int i(0); i < capacity; ++i)
pVertex[i]= false;
}

3, the shortest path SHORTEST PATH

One of the important applications of

, the shortest path.

. Essence Essence How do you feel that writing a blog is so tired? After writing for more than an hour, I wrote so much.

The shortest path behind and the minimum generation tree feels like this for four or five hours.

Write when you have time.

source