Struct pregel_rs::graph_frame::GraphFrame
source · pub struct GraphFrame {
pub vertices: DataFrame,
pub edges: DataFrame,
}
Expand description
The GraphFrame
type is a struct containing two DataFrame
fields, vertices
and edges
.
Properties:
-
vertices
: Thevertices
property is aDataFrame
that represents the nodes in a graph. It must contain a column named Id. -
edges
: Theedges
property is aDataFrame
that represents the edges of a graph. It must contain – at least – two columns: Src and Dst.
Fields§
§vertices: DataFrame
The vertices
property is a DataFrame
that represents the nodes in a graph.
edges: DataFrame
The edges
property is a DataFrame
that represents the edges of a graph.
Implementations§
source§impl GraphFrame
impl GraphFrame
sourcepub fn new(
vertices: DataFrame,
edges: DataFrame
) -> Result<Self, GraphFrameError>
pub fn new( vertices: DataFrame, edges: DataFrame ) -> Result<Self, GraphFrameError>
The function creates a new GraphFrame object with given vertices and edges DataFrames, checking for required columns.
Arguments:
-
vertices
: A DataFrame containing information about the vertices of the graph, such as their IDs and attributes. -
edges
: A DataFrame containing information about the edges in the graph. It should have columns named “src” and “dst” to represent the source and destination vertices of each edge.
Returns:
a Result<Self>
where Self
is the GraphFrame
struct. The Ok
variant of
the Result
contains an instance of GraphFrame
initialized with the provided
vertices
and edges
DataFrames. If any of the required columns (Id
, Src
,
Dst
) are missing in the DataFrames, the function returns an Error
.
Examples found in repository?
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 1, 1, 2, 2, 3],
Object.as_ref() => [1, 0, 3, 1, 3, 2],
]?;
let vertices = df![
VertexId.as_ref() => [0, 1, 2, 3],
Custom("value").as_ref() => [3, 6, 2, 1],
]?;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("max_value"))
.initial_message(col(Custom("value").as_ref()))
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("max_value")),
)
.aggregate_messages(Column::msg(None).max())
.v_prog(max_exprs([
col(Custom("max_value").as_ref()),
Column::msg(None),
]))
.build();
Ok(println!("{}", pregel.run()?))
}
More examples
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 0, 1, 2, 3, 4, 4, 4],
Object.as_ref() => [1, 2, 2, 3, 3, 1, 2, 3],
]?;
let vertices = GraphFrame::from_edges(edges.clone())?.out_degrees()?;
let damping_factor = 0.85;
let num_vertices: f64 = vertices.column(VertexId.as_ref())?.len() as f64;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("rank"))
.initial_message(lit(1.0 / num_vertices))
.send_messages(
MessageReceiver::Subject,
Column::subject(Column::Custom("rank")) / Column::subject(Column::Custom("out_degree")),
)
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("rank")) / Column::subject(Custom("out_degree")),
)
.aggregate_messages(Column::msg(None).sum())
.v_prog(
Column::msg(None) * lit(damping_factor) + lit((1.0 - damping_factor) / num_vertices),
)
.build();
Ok(println!("{}", pregel.run()?))
}
sourcepub fn from_edges(edges: DataFrame) -> Result<Self, GraphFrameError>
pub fn from_edges(edges: DataFrame) -> Result<Self, GraphFrameError>
This function creates a new GraphFrame
from a given set of edges by selecting
source and destination vertices and concatenating them into a unique set of
vertices.
Arguments:
edges
: A DataFrame containing the edges of a graph, with at least two columns named “src” and “dst” representing the source and destination vertices of each edge.
Returns:
The from_edges
function returns a Result<Self>
where Self
is the
GraphFrame
struct.
Examples found in repository?
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 0, 1, 2, 3, 4, 4, 4],
Object.as_ref() => [1, 2, 2, 3, 3, 1, 2, 3],
]?;
let vertices = GraphFrame::from_edges(edges.clone())?.out_degrees()?;
let damping_factor = 0.85;
let num_vertices: f64 = vertices.column(VertexId.as_ref())?.len() as f64;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("rank"))
.initial_message(lit(1.0 / num_vertices))
.send_messages(
MessageReceiver::Subject,
Column::subject(Column::Custom("rank")) / Column::subject(Column::Custom("out_degree")),
)
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("rank")) / Column::subject(Custom("out_degree")),
)
.aggregate_messages(Column::msg(None).sum())
.v_prog(
Column::msg(None) * lit(damping_factor) + lit((1.0 - damping_factor) / num_vertices),
)
.build();
Ok(println!("{}", pregel.run()?))
}
sourcepub fn out_degrees(self) -> PolarsResult<DataFrame>
pub fn out_degrees(self) -> PolarsResult<DataFrame>
This function calculates the out-degree of each node in a graph represented by its edges. The out-degree of a node is defined as the number of out-going edges; that is, edges that have as a source the actual node, and as a destination any other node in a directed-graph.
Returns:
This function returns a Result
containing a DataFrame
. The DataFrame
contains the out-degree of each node in the graph represented by the Graph
object. The original DataFrame
is preserved; that is, we extend it with
the out-degrees of each node.
Examples found in repository?
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
fn main() -> Result<(), Box<dyn Error>> {
let edges = df![
Subject.as_ref() => [0, 0, 1, 2, 3, 4, 4, 4],
Object.as_ref() => [1, 2, 2, 3, 3, 1, 2, 3],
]?;
let vertices = GraphFrame::from_edges(edges.clone())?.out_degrees()?;
let damping_factor = 0.85;
let num_vertices: f64 = vertices.column(VertexId.as_ref())?.len() as f64;
let pregel = PregelBuilder::new(GraphFrame::new(vertices, edges)?)
.max_iterations(4)
.with_vertex_column(Custom("rank"))
.initial_message(lit(1.0 / num_vertices))
.send_messages(
MessageReceiver::Subject,
Column::subject(Column::Custom("rank")) / Column::subject(Column::Custom("out_degree")),
)
.send_messages(
MessageReceiver::Object,
Column::subject(Custom("rank")) / Column::subject(Custom("out_degree")),
)
.aggregate_messages(Column::msg(None).sum())
.v_prog(
Column::msg(None) * lit(damping_factor) + lit((1.0 - damping_factor) / num_vertices),
)
.build();
Ok(println!("{}", pregel.run()?))
}
sourcepub fn in_degrees(self) -> PolarsResult<DataFrame>
pub fn in_degrees(self) -> PolarsResult<DataFrame>
This function calculates the in-degree of each node in a graph represented by its edges. The out-degree of a node is defined as the number of incoming edges; that is, edges that have as a source any node, and as a destination the node itself in a directed-graph.
Returns:
This function returns a Result
containing a DataFrame
. The DataFrame
contains the in-degree of each node in the graph represented by the Graph
object. The original DataFrame
is preserved; that is, we extend it with
the in-degrees of each node.
Trait Implementations§
source§impl Clone for GraphFrame
impl Clone for GraphFrame
source§fn clone(&self) -> GraphFrame
fn clone(&self) -> GraphFrame
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Display for GraphFrame
impl Display for GraphFrame
This is the implementation of the Display
trait for a GraphFrame
struct.
This allows instances of the GraphFrame
struct to be printed in a formatted
way using the println!
macro or other formatting macros. The fmt
method is
defined to format the output as a string that includes the vertices and edges of
the graph.