Map
definition
A tuple, G = (V, E)
V: a finite set of vertices
E: finite set side relationship
into undirected graph (edge) and a directed graph (Arc)
Feature
FIG relationship between the data structure, one kind of mesh nodes is arbitrary, i.e. the figures are likely a direct correlation between any two nodes
Basic terms
Vertex: data element in FIG
edge / arc: the relationship between the two vertices
completely: have n (n-1) / 2 to FIG article without edges
directed entirely: have n (n-1) edges directed graph
dense FIG: there are many sides or arcs of FIG
sparse: have little edges or arcs FIG
subgraph: for G = (V, E) and G '= (V', E '), if V 'is a sub-set of V and E' is a subset of E, called G 'is a subgraph of G.
Right: edge values associated with FIG
Network: FIG Weighted
adjacent point: For G = (V, E), if present (x, y) ∈E, said directed graph without adjacent x and y, there FIG adjacent to said x to y
of the vertices: point associated with the number of sides, and has a degree of penetration into the drawing
path: in G, x elapsed from the vertex list of vertices y reach the top, the said sequence of vertices (x, ..., y) is the path from x to y is
the path length: the number of non-weighted graph path sides, the sides and the upper right of the right picture shows the tape path
circuit (loop): the first vertex last vertex and the same path
communication: in the figure, if there is a path from x to y, x and y are called non-communicating
communication with FIG: undirected graph G, vertex if any two x, y are between is connected, said connected graph is a graph G
connected components: the maximum communication without subgraph is
strong graph: any two of the vertices of the figure x, y are each between reachable, said G in FIG. strongly connected graph is
strongly connected components: directed great communication subgraph in
tree: 1 n-edges minimal connected subgraph (acyclic graph), n vertices,
Simple memory structure of FIG.
Array notation (the adjacency matrix)
① adjacency matrix: matrix representing the relation between the vertices adjacent
edges exist (x, y), i.e., axy = 1 (or weight), or axy = 0 (or ∞)
② characteristics adjacency matrix of
undirected graph adjacency matrix is a symmetric matrix
undirected adjacency matrix row i of FIG / column nonzero / ∞ is the number of elements of the i-th vertex
with a number of non-zero / ∞ elements to the adjacency matrix for the i-th row degrees, as the i-th degree
, such as:
Example:
//使用数组存储图
class SimpleMap{
//顶点个数
public int num;
//图
public int[][] map;
}
public static final int MAX=100000;
/**
* 根据文件内容,生成一个无向图图
* @param file 文件路径
* @return 存储图数据的二阶数组
*/
public static SimpleMap createMap(String file){
SimpleMap sMap = new SimpleMap();
BufferedReader bReader = null;
try {
bReader = new BufferedReader(new FileReader(file));
String data = bReader.readLine();
int number = Integer.parseInt(data);
sMap.num = number;
sMap.map = new int[number][number];
//初始化图
for(int i=0; i<number; i++) {
for(int j=0; j<number; j++) {
sMap.map[i][j] = MAX;
if(i == j) {
sMap.map[i][j] = 0;
}
}
}
//读取文件,初始化边
while((data=bReader.readLine()) != null) {
String[] value = data.split("&");
int x = Integer.parseInt(value[0])-1;
int y = Integer.parseInt(value[1])-1;
int info = Integer.parseInt(value[2]);
sMap.map[x][y] = info;
sMap.map[y][x] = info;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(bReader != null) {
bReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return sMap;
}
Style files are stored:
the number of vertices of the first act, for determining the size of the second order matrix; i.e., 1 behind ---- 2 with a weight of 6
Adjacency list (adjacency lists)
The first node: data (vertex), firstarc (pointer fields)
Table nodes: adjcex (data field), info (weight), nextarc (target domain)
as:
Example:
//使用静态链表存储图
//头结点
class HeadNode{
//结点入度
public int imNum;
//结点数据
public String data;
//结点输出
public MidNode nextIndex;
}
//表结点
class MidNode{
//数据
public String data;
//权值
public int info;
//下一位
public MidNode nextIndex;
}
/**
* 使用静态链表构建一个有向图
* @param file 文件输入
* @return 生成的静态链表
*/
public static HeadNode[] createDirectMap(String file) {
HeadNode[] mapHead = null;
BufferedReader bReader = null;
try {
bReader = new BufferedReader(new FileReader(file));
String data = bReader.readLine();
String[] value = data.split("&");
int number = Integer.parseInt(value[0]);
mapHead = new HeadNode[number];
//初始化静态链表头
for(int i=0; i<number; i++) {
mapHead[i] = new HeadNode();
mapHead[i].imNum = 0;
mapHead[i].data = value[1].substring(i, i+1);
mapHead[i].nextIndex = null;
}
//构建静态链表
while((data = bReader.readLine()) != null) {
value = data.split("&");
String fromData = value[0];
String toData = value[1];
int info = Integer.parseInt(value[2]);
for(int i=0; i<number; i++) {
//当该结点是出点时,新建结点
if(mapHead[i].data.equals(fromData)) {
MidNode mNode = new MidNode();
mNode.data = toData;
mNode.info = info;
//链接
mNode.nextIndex = mapHead[i].nextIndex;
mapHead[i].nextIndex = mNode;
}else if(mapHead[i].data.equals(toData)) {
//当该结点是入点时,入度加一
mapHead[i].imNum ++;
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(bReader != null) {
bReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return mapHead;
}
Style files are stored:
the vertex number of the vertex of a first name & Behavior; i.e. behind A-> B, a weight of 1