({{, (RefLink[SetProperty, paclet:ref/SetProperty])[{obj,itemspec},name->value] 
sets the property name->value for itemspec in obj.}})

OpenersNotesSection(Spacer1)Details

Properties are used to store and manipulate name->value pairs for specific named items within an object such as (RefLink[Graph, paclet:ref/Graph]).

(RefLink[SetProperty, paclet:ref/SetProperty]) will produce an object with a modified property value.

The item specification itemspec typically has the form {collection,index}.

The index can typically have the following forms:

The collection can typically have the following forms:

The item specification itemspec of the form collection refers to the default value for all indexes in the collection.

The items available for (RefLink[Graph, paclet:ref/Graph]) objects include vertices and edges.

OpenersPrimaryExamplesSection(Spacer1)Examples  (18)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Specify a vertex or edge property:

g=CompleteGraph[4]

{SetProperty[{g,1},VertexLabels->"one"],SetProperty[{g,1\[UndirectedEdge]2},EdgeStyle->Brown]}

Use (RefLink[SetProperty, paclet:ref/SetProperty]) to override the vertex style:

CompleteGraph[4,VertexSize->Small]

SetProperty[{%,1},VertexStyle->Red]

SetProperty[{%,1},VertexStyle->Blue]

OpenersExampleSection1(Spacer1)Scope  (6)

OpenersExampleSubsection0(Spacer1)Basic Uses  (2)

Set properties for vertices:

SetProperty[{,1},VertexSize->Small]

And edges:

SetProperty[{,1\[UndirectedEdge]2},EdgeLabels->"hello"]

Set several properties for a vertex:

SetProperty[{,1},{VertexStyle->Magenta,VertexLabels->"hello"}]

And for an edge:

SetProperty[{,1\[UndirectedEdge]2},{EdgeStyle->Magenta,EdgeLabels->"hello"}]

OpenersExampleSubsection1(Spacer1)Vertex Properties  (2)

Standard vertex properties include (RefLink[VertexStyle, paclet:ref/VertexStyle]):

g=CompleteGraph[4,VertexSize->Medium]

SetProperty[{g,4},VertexStyle->Red]

(RefLink[VertexLabels, paclet:ref/VertexLabels]):

SetProperty[{g,1},VertexLabels->"one"]

(RefLink[VertexSize, paclet:ref/VertexSize]):

SetProperty[{g,1},VertexSize->Tiny]

(RefLink[VertexShape, paclet:ref/VertexShape]):

SetProperty[{g,1},VertexShape->]

(RefLink[VertexShapeFunction, paclet:ref/VertexShapeFunction]):

SetProperty[{g,2},VertexShapeFunction->"Square"]

Non-visual properties include (RefLink[VertexWeight, paclet:ref/VertexWeight]):

SetProperty[{g,1},VertexWeight->100]

PropertyValue[{%,1},VertexWeight]

Set a custom property:

SetProperty[{,2},"Material"->"Wood"]

PropertyValue[{%,2},"Material"]

OpenersExampleSubsection2(Spacer1)Edge Properties  (2)

Standard edge properties include (RefLink[EdgeStyle, paclet:ref/EdgeStyle]):

g=CompleteGraph[4]

SetProperty[{g,1\[UndirectedEdge]2},EdgeStyle->Brown]

(RefLink[EdgeLabels, paclet:ref/EdgeLabels]):

SetProperty[{g,1\[UndirectedEdge]2},EdgeLabels->1\[UndirectedEdge]2]

(RefLink[EdgeShapeFunction, paclet:ref/EdgeShapeFunction]):

SetProperty[{g,3\[UndirectedEdge]4},EdgeShapeFunction->"HalfFilledDoubleArrow"]

Non-visual properties include (RefLink[EdgeWeight, paclet:ref/EdgeWeight]):

SetProperty[{g,1\[UndirectedEdge]2},EdgeWeight->42]

PropertyValue[{%,1\[UndirectedEdge]2},EdgeWeight]

Set a custom property:

SetProperty[{,2\[UndirectedEdge]4},"Resistance"->50.]

PropertyValue[{%,2\[UndirectedEdge]4},"Resistance"]

OpenersExampleSection3(Spacer1)Applications  (3)

Use (RefLink[Fold, paclet:ref/Fold]) to apply (RefLink[SetProperty, paclet:ref/SetProperty]) repeatedly:

g=CycleGraph[8,VertexSize->Large]

Fold[SetProperty[{#1,#2},VertexStyle->ColorData[45,#2]]&,g,Range[1,5]]

See all the partial results:

FoldList[SetProperty[{#1,#2},VertexStyle->ColorData[45,#2]]&,g,Range[1,6]]

Create a graph:

g=CompleteGraph[4]

Temporarily highlight one vertex:

SetProperty[{g,1},VertexSize->Medium]

Then another:

SetProperty[{g,2},VertexSize->Medium]

Highlight the edges visited during a breadth-first scan:

g=CompleteKaryTree[4,3,DirectedEdges->True]

ListAnimate[Reap[BreadthFirstScan[g,{"FrontierEdge"->(Sow[SetProperty[{g,#},EdgeStyle->Red]]& )}]][[2,1]],DefaultDuration->Length[EdgeList[g]]/2,SaveDefinitions->True]

OpenersExampleSection4(Spacer1)Properties & Relations  (6)

Use (RefLink[Property, paclet:ref/Property]) as a wrapper to specify properties in functions such as (RefLink[Graph, paclet:ref/Graph]):

Graph[{Property[1,{VertexSize->Medium,VertexStyle->Red}],2,3},{1\[UndirectedEdge]2,2\[UndirectedEdge]3,3\[UndirectedEdge]1}]

Use (RefLink[PropertyValue, paclet:ref/PropertyValue]) to extract values:

g=CompleteGraph[4,VertexStyle->{1->Red},VertexSize->Small]

PropertyValue[{g,1},VertexStyle]

For undefined properties, the value (RefLink[$Failed, paclet:ref/$Failed]) is returned:

PropertyValue[{g,1},"foo"]

MemberQ[PropertyList[{g,1}],"foo"]

Use (RefLink[PropertyValue, paclet:ref/PropertyValue]) to set properties in a graph:

g=CompleteGraph[4]

PropertyValue[{g,1},VertexSize]=Small;

The original graph has changed:

g

Use (RefLink[PropertyValue, paclet:ref/PropertyValue]) to unset properties in a graph:

g=CompleteGraph[4,VertexSize->{1->Small}]

PropertyValue[{g,1},VertexSize]=.;

The original graph has changed:

g

Use (RefLink[PropertyList, paclet:ref/PropertyList]) to test for the presence of a property:

g=CompleteGraph[4,VertexLabels->{1->one,4->four}]

MemberQ[PropertyList[{g,1}],VertexLabels]

Generate a list of vertex label rules:

Cases[VertexList[g],v_/;MemberQ[PropertyList[{g,v}],VertexLabels]:>v->PropertyValue[{g,v},VertexLabels]]

Use (RefLink[RemoveProperty, paclet:ref/RemoveProperty]) to remove a property without changing the original:

g=CompleteGraph[4,VertexStyle->{1->Red},VertexSize->Small]

RemoveProperty[{g,1},VertexStyle]

The original graph is unchanged:

g

OpenersExampleSection5(Spacer1)Possible Issues  (1)

(RefLink[PropertyValue, paclet:ref/PropertyValue]) is typically faster than (RefLink[SetProperty, paclet:ref/SetProperty]):

g=CycleGraph[1000];

Timing@Do[PropertyValue[{g,v},VertexWeight]=RandomReal[],{v,VertexList[g]}]

g=CycleGraph[1000];

Timing@Do[g=SetProperty[{g,v},VertexWeight->RandomReal[]],{v,VertexList[g]}]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[PropertyList, paclet:ref/PropertyList])  ▪  (RefLink[PropertyValue, paclet:ref/PropertyValue])  ▪  (RefLink[RemoveProperty, paclet:ref/RemoveProperty])  ▪  (RefLink[Property, paclet:ref/Property])  ▪  (RefLink[Properties, paclet:ref/Properties])  ▪  (RefLink[Graph, paclet:ref/Graph])  ▪  (RefLink[MeshRegion, paclet:ref/MeshRegion])  ▪  (RefLink[BoundaryMeshRegion, paclet:ref/BoundaryMeshRegion])  ▪  (RefLink[SetOptions, paclet:ref/SetOptions])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Graph Properties, paclet:guide/GraphProperties])}, {▪(RefLinkPlain[Graph Programming, paclet:guide/GraphProgramming])}, {▪(RefLinkPlain[Properties, paclet:guide/Properties])}})}})