11 Nisan 2020 Cumartesi

GraphBuilder Sınıfı

Giriş
Şu satırı dahil ederiz.
import com.google.common.graph.ElementOrder;
import com.google.common.graph.Graph;
import com.google.common.graph.GraphBuilder;
import com.google.common.graph.Traverser;
MutableGraph nesneyi yaratmak için kullanılır

directed metodu
Şöyle yaparız
//graph of Integers
MutableGraph<Integer> myGraph= GraphBuilder.directed().build();
//graph of Strings
MutableGraph<String> myGraph= GraphBuilder.directed().build();
//graph of any classes
MutableGraph<MyCustomClassInstance> myGraph= GraphBuilder.directed().build();
nodeOrder metodu - ElementOrder.insertion
Açıklaması şöyle. Düğümleri eklendikleri sırada dolaşabilme imkanı sağlar.
Graph stores nodes in map under the hood, and for ElementOrder.insertion() it's LinkedHashMap. For Graph#nodes() keySet() of such map is used, so there's no better way to fetch last value other than iterating over all elements and returning the last one.
Şöyle yaparız.
MutableGraph<Integer> subgraph = GraphBuilder.undirected()
  .nodeOrder(ElementOrder.insertion())
  .build();
putEdge metodu
Örnek
Şöyle yaparız
Node root = new Node("root");
Graph<Node> graph = GraphBuilder.directed()
  .nodeOrder(ElementOrder.insertion())
  .<Node>immutable()
  .putEdge(root, new Node("one"))
  .putEdge(root, new Node("two"))
  .putEdge(root, new Node("three"))
  .build();

//Print the nodes in traversal order.
Traverser.forGraph(graph).depthFirstPostOrder(root)
  .forEach(x->System.out.println(x));

Hiç yorum yok:

Yorum Gönder