Where to Find Up-to-date yFiles Information

This page is from the outdated yFiles for Java 2.13 documentation. You can find the most up-to-date documentation for all yFiles products on the yFiles documentation overview page.

Please see the following links for more information about the yFiles product family of diagramming programming libraries and corresponding yFiles products for modern web apps, for cross-platform Java(FX) applications, and for applications for the Microsoft .NET environment.

More about the yFiles product family Close X

Chapter 4. Working with the Graph Structure

Table of Contents

Creating Graphs and Graph Elements
Graph Structure Functionality
More About Class Graph
Graph Elements
Class Node
Class Edge
Complexity
Advanced Topics
Hiding Graph Elements
Copying a Graph
Events and Listeners
Structural Changes
Iteration Mechanisms
Iteration and Element Removal
Alternative Iteration Techniques
Containers
High-Level Features
Low-Level Features
Binding Data to Graph Elements
Maps and Data Providers
Default Map Implementations
Creating Customized Data Accessors
Notes
Analyzing Graphs
Quickly Checking for Graph Characteristics
Advanced Graph Characteristics
Breadth-First Search
Depth-First Search
Graph Connectivity
Shortest Paths
Centrality Measures
Spanning Trees
Transitivity
Trees

This chapter presents first steps to using the yFiles graph structure and outlines general programming aspects. It explains technical consequences of some of the concepts in more detail. Also, it introduces well-known graph analyzing algorithms indispensable to the serious graph worker.

Creating Graphs and Graph Elements

Class Graph offers three constructors, one to create an empty graph, i.e., one where node and edge set are empty, the other two to create new instances which are either partial or complete copies of an already existing graph.

// Create a new, empty graph. 
Graph graph = new Graph();

// Create a complete copy of 'graph'. 
Graph graphCopy = new Graph(graph);

It is also possible to ask an instance of type Graph to create a complete copy of itself or to create a new empty graph instance.

When populating an existing graph with graph elements, it is important to observe certain preconditions:

  • Only class Graph provides methods to create nodes and edges.
  • To create an edge in a graph G it is necessary that both its source node and its target node already exist in G.

// 'graph' is of type y.base.Graph. 

// Create 10 nodes. 
Node n[] = new Node[10];
for (int i = 0; i < 10; i++)
  n[i] = graph.createNode();

// Create 5 edges. Each edge has "even" source node and "odd" target node. 
Edge e[] = new Edge[5];
for (int i = 0, j = 0; i < 10; i += 2, j++)
  e[j] = graph.createEdge(n[i], n[i + 1]);

For further code examples see also GraphDemo.java and RandomGraphGenerator.java from the tutorial demo applications.

Important

Note that the Java code snippets presented in the course of this document serve as examples to demonstrate how to use the functionality yFiles provides. Although they are all correct, they are not complete and will most likely not compile when copied directly.