#! /usr/bin/env python3 # def graph_arc_test ( ): #*****************************************************************************80 # ## graph_arc_test() tests graph_arc(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 14 April 2026 # # Author: # # John Burkardt # import numpy as np import platform print ( '' ) print ( 'graph_arc_test():' ) print ( ' numpy version: ' + np.version.version ) print ( ' python version: ' + platform.python_version ( ) ) print ( ' Test graph_arc().' ) graph_adj_to_arc_test ( ) graph_arc_to_adj_test ( ) # # Terminate. # print ( '' ) print ( 'graph_arc_test():' ) print ( ' Normal end of execution.' ) return def graph_adj_to_arc ( A ): #*****************************************************************************80 # ## graph_adj_to_arc() converts an adjacency graph to an arc list graph. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 April 2026 # # Author: # # John Burkardt # # Input: # # integer A(node_num,node_num), the adjacency matrix. # # Output: # # integer inode(edge_num), jnode(edge_num): the arc list of the graph. # import numpy as np edge_num = np.sum ( A ) node_num = A.shape[0] edge_id = 0 inode = -1 * np.ones ( edge_num, dtype = int ) jnode = -1 * np.ones ( edge_num, dtype = int ) for i in range ( 0, node_num ): for j in range ( 0, node_num ): if ( A[i,j] != 0 ): inode[edge_id] = i jnode[edge_id] = j edge_id = edge_id + 1 return inode, jnode def graph_adj_to_arc_test ( ): #*****************************************************************************80 # ## graph_adj_to_arc_test() tests graph_adj_to_arc(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 April 2026 # # Author: # # John Burkardt # import numpy as np import pprint node_num = 13 A = np.array ( [ \ [0,1,1,1,1,1,1,1,0,0,0,0,0], \ [1,0,0,0,1,1,0,1,0,0,0,0,0], \ [1,0,0,1,0,0,1,0,0,0,0,0,0], \ [1,0,1,0,0,0,0,0,0,0,0,0,0], \ [1,1,0,0,0,0,0,0,0,0,0,0,0], \ [1,1,0,0,0,0,0,0,0,0,0,0,0], \ [1,0,1,0,0,0,0,0,0,0,0,0,0], \ [1,1,0,0,0,0,0,0,1,0,0,0,0], \ [0,0,0,0,0,0,0,1,0,1,0,0,1], \ [0,0,0,0,0,0,0,0,1,0,1,1,1], \ [0,0,0,0,0,0,0,0,0,1,0,1,0], \ [0,0,0,0,0,0,0,0,0,1,1,0,0], \ [0,0,0,0,0,0,0,0,1,1,0,0,0] ], dtype = int ) print ( '' ) print ( 'graph_adj_to_arc_test():' ) print ( ' graph_adj_to_arc() converts a graph adjacency' ) print ( ' representation of a graph to graph arc format.' ) print ( '' ) print ( ' The graph adjacency matrix:' ) pprint.pprint ( A ) edge_num = np.sum ( A ) print ( '' ) print ( ' Number of edges is ', edge_num ) inode, jnode = graph_adj_to_arc ( A ) knode = np.transpose ( np.stack ( ( inode, jnode ) ) ) print ( '' ) print ( ' The arc list:' ) pprint.pprint ( knode ) return def graph_arc_to_adj ( inode, jnode ): #*****************************************************************************80 # ## graph_arc_to_adj() converts an arc list graph to an adjacency graph. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 April 2026 # # Author: # # John Burkardt # # Input: # # integer INODE(edge_num), JNODE(edge_num), the edge array for # an undirected graph. The I-th edge connects nodes INODE(I) and JNODE(I). # # Output: # # integer A(node_num,node_num), the adjacency information. # import numpy as np edge_num = len ( inode ) node_num = max ( np.max ( inode ), np.max ( jnode ) ) + 1 A = np.zeros ( [ node_num, node_num ] ) for edge_id in range ( 0, edge_num ): i = inode[edge_id] j = jnode[edge_id] A[i,j] = 1 return A def graph_arc_to_adj_test ( ): #*****************************************************************************80 # ## graph_arc_to_adj_test() tests graph_arc_to_adj(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 10 April 2026 # # Author: # # John Burkardt # import numpy as np import pprint inode = np.array ( [ \ 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, \ 2, 3, 3, 3, 4, 4, 5, 5, 6, 6, \ 7, 7, 8, 8, 8, 9, 9, 9,10,10, \ 10,10,11,11,12,12,13,13 ], dtype = int ) jnode = np.array ( [ \ 2, 3, 4, 5, 6, 7, 8, 1, 5, 6, \ 8, 1, 4, 7, 1, 3, 1, 2, 1, 2, \ 1, 3, 1, 2, 9, 8,10,13, 9,11, \ 12,13,10,12,10,11, 9,10 ], dtype = int ) # # Adjust to zero-based indexing. # inode = inode - 1 jnode = jnode - 1 print ( '' ) print ( 'graph_arc_to_adj_test():' ) print ( ' graph_arc_to_adj() converts a graph arc' ) print ( ' representation of a graph to an adjacency matrix.' ) knode = np.transpose ( np.stack ( ( inode, jnode ) ) ) print ( '' ) print ( ' The arc list:' ) pprint.pprint ( knode ) A = graph_arc_to_adj ( inode, jnode ) print ( '' ) print ( ' The graph adjacency matrix:' ) pprint.pprint ( A ) return def timestamp ( ): #*****************************************************************************80 # ## timestamp() prints the date as a timestamp. # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 06 April 2013 # # Author: # # John Burkardt # import time t = time.time ( ) print ( time.ctime ( t ) ) return if ( __name__ == "__main__" ): timestamp ( ) graph_arc_test ( ) timestamp ( )