Skip to content

Commit

Permalink
Update sparse matrices to sparse arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbowly committed Oct 18, 2023
1 parent dcc340b commit 3decfef
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions docs/source/mods/max-flow-min-cut.rst
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ An example of these inputs with their respective requirements is shown below.
>>> G, capacities, _, _ = datasets.simple_graph_scipy()
>>> G.data = capacities.data # Copy capacity data
>>> G
<5x6 sparse matrix of type '<class 'numpy.int64'>'
<5x6 sparse array of type '<class 'numpy.int64'>'
with 7 stored elements in COOrdinate format>
>>> print(G)
(0, 1) 2
Expand All @@ -140,7 +140,7 @@ An example of these inputs with their respective requirements is shown below.
(3, 5) 2
(4, 5) 2

We only need the adjacency matrix for the graph (as sparse matrix) where
We only need the adjacency matrix for the graph (as a sparse array) where
each each entry contains the capacity of the edge.

|
Expand Down Expand Up @@ -207,7 +207,7 @@ Let us use the data to solve the maximum flow problem.
>>> obj
3.0
>>> sol
<5x6 sparse matrix of type '<class 'numpy.float64'>'
<5x6 sparse array of type '<class 'numpy.float64'>'
with 6 stored elements in COOrdinate format>
>>> print(sol)
(0, 1) 1.0
Expand All @@ -218,7 +218,7 @@ Let us use the data to solve the maximum flow problem.
(4, 5) 2.0

The ``max_flow`` function returns the value of the maximum flow as well a
sparse matrix with the amount of non-zero flow in each edge in the
sparse array with the amount of non-zero flow in each edge in the
solution.

The solution for this example is shown in the figure below. The edge labels
Expand Down
8 changes: 4 additions & 4 deletions src/gurobi_optimods/max_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,19 @@ def _max_flow_scipy(G, source, sink, **kwargs):
data = np.append(G.data, max_flow)
from_arc = np.append(G.row, sink)
to_arc = np.append(G.col, source)
G = sp.coo_matrix((data, (from_arc, to_arc)), dtype=float)
G = sp.coo_array((data, (from_arc, to_arc)), dtype=float)

capacities = sp.coo_matrix(G)
capacities = sp.coo_array(G)

costs = np.zeros(G.row.shape, dtype=float)
costs[-1] = -1
costs = sp.coo_matrix((costs, (G.row, G.col)), dtype=float)
costs = sp.coo_array((costs, (G.row, G.col)), dtype=float)
demands = np.zeros(G.shape[1], dtype=float)
# Solve
obj, flow = min_cost_flow_scipy(G, capacities, costs, demands, **kwargs)
G = _remove_dummy_edge(G, source, sink)
flow = _remove_dummy_edge(flow, source, sink)
return -obj, sp.coo_matrix(flow)
return -obj, sp.coo_array(flow)


def _max_flow_networkx(G, source, sink, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions src/gurobi_optimods/min_cut.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def _min_cut_scipy(G, source, sink, create_env):

from_arc = np.append(G.row, sink)
to_arc = np.append(G.col, source)
G = sp.coo_matrix((data, (from_arc, to_arc)), dtype=float)
G = sp.coo_array((data, (from_arc, to_arc)), dtype=float)

capacities = data

Expand All @@ -170,7 +170,7 @@ def _min_cut_scipy(G, source, sink, create_env):
ones = np.ones(from_arc.shape)
data = np.column_stack((ones * -1.0, ones)).reshape(-1, order="C")

A = sp.csc_matrix((data, indices, indptr))
A = sp.csc_array((data, indices, indptr))

logger.info(
f"Solving min-cut problem with {A.shape[0]} nodes and " f"{A.shape[1]-1} edges"
Expand Down

0 comments on commit 3decfef

Please sign in to comment.