Module graphml

Module graphml 

Source
Expand description

GraphML format I/O for graphs

This module provides functionality for reading and writing graphs in GraphML format. GraphML is an XML-based format for representing graph structures with rich metadata and attribute support, widely used in graph analysis tools.

§Format Specification

GraphML uses XML structure with the following key elements:

  • <graphml> - Root element with namespace declarations
  • <key> - Attribute definitions for nodes/edges
  • <graph> - Graph container with id and directedness
  • <node> - Node elements with ids and data attributes
  • <edge> - Edge elements with source/target and data attributes
  • <data> - Data elements containing attribute values

§Examples

§Basic GraphML structure:

<?xml version="1.0" encoding="UTF-8"?>
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://graphml.graphdrawing.org/xmlns
         http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd">
  <key id="weight" for="edge" attr.name="weight" attr.type="double"/>
  <graph id="G" edgedefault="undirected">
    <node id="1"/>
    <node id="2"/>
    <edge id="e1" source="1" target="2">
      <data key="weight">1.5</data>
    </edge>
  </graph>
</graphml>

§Usage

use std::fs::File;
use std::io::Write;
use tempfile::NamedTempFile;
use scirs2_graph::base::Graph;
use scirs2_graph::io::graphml::{read_graphml_format, write_graphml_format};

// Create a temporary file with GraphML data
let mut temp_file = NamedTempFile::new().unwrap();
writeln!(temp_file, r#"<?xml version="1.0" encoding="UTF-8"?>"#).unwrap();
writeln!(temp_file, r#"<graphml xmlns="http://graphml.graphdrawing.org/xmlns">"#).unwrap();
writeln!(temp_file, r#"  <graph id="G" edgedefault="undirected">"#).unwrap();
writeln!(temp_file, r#"    <node id="1"/>"#).unwrap();
writeln!(temp_file, r#"    <node id="2"/>"#).unwrap();
writeln!(temp_file, r#"    <edge source="1" target="2"/>"#).unwrap();
writeln!(temp_file, r#"  </graph>"#).unwrap();
writeln!(temp_file, r#"</graphml>"#).unwrap();
temp_file.flush().unwrap();

// Read the graph
let graph: Graph<i32, f64> = read_graphml_format(temp_file.path(), false).unwrap();
assert_eq!(graph.node_count(), 2);
assert_eq!(graph.edge_count(), 1);

Functions§

read_graphml_format
Read an undirected graph from GraphML format
read_graphml_format_digraph
Read a directed graph from GraphML format
write_graphml_format
Write an undirected graph to GraphML format
write_graphml_format_digraph
Write a directed graph to GraphML format