## Class UndirectedGraph

• All Implemented Interfaces:
`IGraph`
Direct Known Subclasses:
`G`

```public class UndirectedGraph
extends Object
implements IGraph```
Specific implementation of an undirected graph
Author:
Jean-Guillaume Fages, Xavier Lorca
• ### Constructor Summary

Constructors
Constructor Description
```UndirectedGraph​(int n, SetType type, boolean allNodes)```
Creates an empty (non-backtrackable) undirected graph.
```UndirectedGraph​(Model model, int n, SetType type, boolean allNodes)```
Creates an empty backtrable undirected graph.
• ### Method Summary

All Methods
Modifier and Type Method Description
`boolean` ```addEdge​(int x, int y)```
Add edge (x,y) to the graph
`boolean` `addNode​(int x)`
Adds node x to the node set of the graph
`boolean` ```edgeExists​(int x, int y)```
test whether edge (x,y) is in the graph or not
`int` `getNbMaxNodes()`
The maximum number of nodes in the graph Vertices of the graph belong to [0,getNbMaxNodes()-1] This quantity is fixed at the creation of the graph
`ISet` `getNeighOf​(int x)`
Get neighbors of node x
`ISet` `getNodes()`
`ISet` `getPredOrNeighOf​(int x)`
Get either x's predecessors or neighbors.
`ISet` `getSuccOrNeighOf​(int x)`
Get either x's successors or neighbors.
`SetType` `getType()`
Get the type of data structures used in the graph
`boolean` ```isArcOrEdge​(int x, int y)```
If `this ` is directed returns true if and only if arc (x,y) exists Else, if `this` is undirected returns true if and only if edge (x,y) exists
`boolean` `isDirected()`
`boolean` ```removeEdge​(int x, int y)```
Remove edge (x,y) from the graph
`boolean` `removeNode​(int x)`
Remove node x from the graph
`String` `toString()`
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`
• ### Methods inherited from interface org.chocosolver.util.objects.graphs.IGraph

`graphVizExport`
• ### Constructor Detail

• #### UndirectedGraph

```public UndirectedGraph​(Model model,
int n,
SetType type,
boolean allNodes)```
Creates an empty backtrable undirected graph. Allocates memory for n nodes (but they should then be added explicitly, unless allNodes is true).
Parameters:
`model` - model providing the backtracking environment
`n` - max number of nodes
`type` - data structure storing for node neighbors
`allNodes` - true iff all nodes will always remain in the graph
• #### UndirectedGraph

```public UndirectedGraph​(int n,
SetType type,
boolean allNodes)```
Creates an empty (non-backtrackable) undirected graph. Allocates memory for n nodes (but they should then be added explicitly, unless allNodes is true).
Parameters:
`n` - max number of nodes
`type` - data structure used for storing node neighbors
`allNodes` - true iff all nodes will always remain in the graph
• ### Method Detail

• #### toString

`public String toString()`
Overrides:
`toString` in class `Object`
• #### getNbMaxNodes

`public int getNbMaxNodes()`
Description copied from interface: `IGraph`
The maximum number of nodes in the graph Vertices of the graph belong to [0,getNbMaxNodes()-1] This quantity is fixed at the creation of the graph
Specified by:
`getNbMaxNodes` in interface `IGraph`
Returns:
the maximum number of nodes of the graph
• #### getNodes

`public ISet getNodes()`
Specified by:
`getNodes` in interface `IGraph`
Returns:
the collection of nodes present in the graph
• #### getType

`public SetType getType()`
Description copied from interface: `IGraph`
Get the type of data structures used in the graph
Specified by:
`getType` in interface `IGraph`
Returns:
the type of data structures used in the graph

`public boolean addNode​(int x)`
Description copied from interface: `IGraph`
Adds node x to the node set of the graph
Specified by:
`addNode` in interface `IGraph`
Parameters:
`x` - a node index
Returns:
true iff x was not already present in the graph
• #### removeNode

`public boolean removeNode​(int x)`
Description copied from interface: `IGraph`
Remove node x from the graph
Specified by:
`removeNode` in interface `IGraph`
Parameters:
`x` - a node index
Returns:
true iff x was present in the graph

```public boolean addEdge​(int x,
int y)```
Add edge (x,y) to the graph
Parameters:
`x` - a node index
`y` - a node index
Returns:
true iff (x,y) was not already in the graph
• #### edgeExists

```public boolean edgeExists​(int x,
int y)```
test whether edge (x,y) is in the graph or not
Parameters:
`x` - a node index
`y` - a node index
Returns:
true iff edge (x,y) is in the graph
• #### isArcOrEdge

```public boolean isArcOrEdge​(int x,
int y)```
Description copied from interface: `IGraph`
If `this ` is directed returns true if and only if arc (x,y) exists Else, if `this` is undirected returns true if and only if edge (x,y) exists

This method enables to capitalize some code but should be called with care

Specified by:
`isArcOrEdge` in interface `IGraph`
Parameters:
`x` - a node index
`y` - a node index
• #### removeEdge

```public boolean removeEdge​(int x,
int y)```
Remove edge (x,y) from the graph
Parameters:
`x` - a node index
`y` - a node index
Returns:
true iff (x,y) was in the graph
• #### getNeighOf

`public ISet getNeighOf​(int x)`
Get neighbors of node x
Parameters:
`x` - node index
Returns:
neighbors of x (predecessors and/or successors)
• #### getPredOrNeighOf

`public ISet getPredOrNeighOf​(int x)`
Description copied from interface: `IGraph`
Get either x's predecessors or neighbors.

This method enables to capitalize some code but should be called with care

Specified by:
`getPredOrNeighOf` in interface `IGraph`
Parameters:
`x` - a node index
Returns:
x's predecessors if `this` is directed x's neighbors otherwise
• #### getSuccOrNeighOf

`public ISet getSuccOrNeighOf​(int x)`
Description copied from interface: `IGraph`
Get either x's successors or neighbors.

This method enables to capitalize some code but should be called with care

Specified by:
`getSuccOrNeighOf` in interface `IGraph`
Parameters:
`x` - a node index
Returns:
x's successors if `this` is directed x's neighbors otherwise
• #### isDirected

`public boolean isDirected()`
Specified by:
`isDirected` in interface `IGraph`
Returns:
true if and only if `this` is a directed graph