Graph Data Structures - Description of Graph Structures

a picture

1. Description of the figure

       In computer applications, we build models to represent the relationships represented by connected nodes, and the connections between these nodes naturally lead to a series of questions: whether along these connections can go from one node to another point,

How many nodes are connected to each other? Which one is the shortest path between two nodes?

       To describe these problems, we use an abstract data model - the graph data model. Using this model, we can solve many real-world problems, such as the problem of lines between two cities in the map, various components in the circuit board and Problems with connecting wires, matching students with schools, etc.

      The classification of graphs is roughly divided into: undirected graphs, directed graphs, weighted graphs and weighted directed graphs

      Definition of a graph: A graph consists of a set of vertices and a set of edges that connect two vertices

     Special forms of graphs: (1) self-loop, i.e. an edge connecting a vertex to itself (2) two parallel edges connecting two vertices 

    But we won't discuss this special form, we can represent the edge with two vertices.

    Some terms for graphs

   (1) Adjacent: Two vertices connected by an edge are adjacent, also called two vertices attached to this edge

   (2) Degree: The number of all edges attached to a vertex is the degree of a vertex

   (3) Subgraph: a subset composed of all edges

   (4) Path: a series of vertices connected in sequence by edges, a simple path is a path without repeated fixed points

  Several representations of graphs

  (1) Adjacency matrix, we can use the boolean matrix of v times v, the edge definition value between vertex v and vertex w is true, otherwise it is false

      But this condition cannot meet the requirement of tens of millions of vertices

    (2) An array of edges, we can use a class Edge, which contains instance variables of two vertices, but this representation method is very inconvenient to implement adjacent edge operations

    (3) Adjacency list array, we can use a vertex-indexed list array, where each element is a list of vertices connected to the vertex

     Here is my way of using adjacency representation

2. Code representation of undirected graph

        

    

import com.lxy.datastructure.bag.Bag;

import edu.princeton.cs.introcs.In;

/**
 * Undirected graph
 * @author lxy
 *
 */
public class Graph {
 private static final String NEWLINE = System.getProperty("line.separator");
 private int V;//Number of vertices
 private int E;//Number of sides
 private Bag<Integer>[] adj;//Adjacency list
 
 public Graph(int V) {
    this.V=V;
    adj=(Bag<Integer>[])new Bag[V];
    for(int v=0;v<V;v++)
    	adj[v]=new Bag<Integer>();
 }
 public Graph(In in) {
    this(in.readInt());
    int E=in.readInt();
    for(int i=0;i<E;i++){
      int v=in.readInt();
      int w=in.readInt();
      addEdge(v,w);
    }
 }
 /**
  * add an edge vw
  * @paramv
  * @param w
  */
 public void addEdge(int v, int w) {
   adj[v].add(w);
   adj[w].add(v);
   E++;
 }
public int getE() {
	return E;
 }
 public int getV() {
	return V;
 }
 /**
  * all edges adjacent to v vertex
  * @paramv
  * @return
  */
 public Iterable<Integer> adj(int v){
	 return adj[v];
 }
 public String toString() {
     StringBuilder s = new StringBuilder();
     s.append(V + " vertices, " + E + " edges " + NEWLINE);
     for (int v = 0; v < V; v++) {
         s.append(v + ": ");
         for (int w : adj[v]) {
             s.append(w + " ");
         }
         s.append(NEWLINE);
     }
     return s.toString();
 }
}

 

 
package com.lxy.datastructure.bag;

import java.util.Arrays;
import java.util.Iterator;

public class Bag<T> implements Iterable<T>{
  private int N;
  private T[] elementData;
  public Bag() {
       elementData=(T[])new Object[16];
  }
 public void add(T object) {
    if(elementData.length==N)resize(N*2);
     elementData[N++]=object;
 }

 public void resize(int m){
	 T[] newElementData=(T[]) new Object[m];
     System.arraycopy(elementData,0,newElementData,0,N);
     this.elementData=newElementData;
 }
 
 
@Override
public Iterator<T> iterator() {
	return Arrays.asList(elementData).subList(0,N).iterator();
}
public boolean isEmpty(){

	return size()==0;
}
public int size(){
	return N;
}

}
 
public class GraphTest {
	String basePath=System.getProperty("user.dir");
    File file1=null;
    File file2=null;
    Graph g1 =null;
    @Before
	public void setup(){
		file1=new File(basePath,"data/graph/mediumG.txt");
		file2=new File(basePath,"data/graph/tinyG.txt");
		In in = new In(file2);
		g1=new Graph(in);
		
		
	}  
    @Test
    public void testPrintGraph(){
    	System.out.println(g1.toString());
    }
    
	
}
13 vertices, 13 edges
0: 5 1 2 6
1: 0
2: 0
3: 4 5
4: 3 6 5
5: 0 4 3
6: 4 0
7: 8
8: 7
9: 12 10 11
10: 9
11: 12 9
12: 9 11
 Third-party jar, test case data in the attachment

 

 

  

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326870754&siteId=291194637