annotate mayachemtool/mayachemtools/lib/Graph/PathGraph.pm @ 0:68300206e90d draft default tip

Uploaded
author deepakjadmin
date Thu, 05 Nov 2015 02:41:30 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
1 package Graph::PathGraph;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
2 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
3 # $RCSfile: PathGraph.pm,v $
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
4 # $Date: 2015/02/28 20:49:06 $
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
5 # $Revision: 1.24 $
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
6 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
7 # Author: Manish Sud <msud@san.rr.com>
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
8 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
9 # Copyright (C) 2015 Manish Sud. All rights reserved.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
10 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
11 # This file is part of MayaChemTools.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
12 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
13 # MayaChemTools is free software; you can redistribute it and/or modify it under
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
14 # the terms of the GNU Lesser General Public License as published by the Free
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
15 # Software Foundation; either version 3 of the License, or (at your option) any
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
16 # later version.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
17 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
18 # MayaChemTools is distributed in the hope that it will be useful, but without
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
19 # any warranty; without even the implied warranty of merchantability of fitness
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
20 # for a particular purpose. See the GNU Lesser General Public License for more
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
21 # details.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
22 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
23 # You should have received a copy of the GNU Lesser General Public License
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
24 # along with MayaChemTools; if not, see <http://www.gnu.org/licenses/> or
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
25 # write to the Free Software Foundation Inc., 59 Temple Place, Suite 330,
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
26 # Boston, MA, 02111-1307, USA.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
27 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
28
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
29 use strict;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
30 use Carp;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
31 use Exporter;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
32 use Storable ();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
33 use Scalar::Util ();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
34 use Graph;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
35 use Graph::Path;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
36
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
37 use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
38
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
39 @ISA = qw(Graph Exporter);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
40 @EXPORT = qw(IsPathGraph);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
41 @EXPORT_OK = qw();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
42
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
43 %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
44
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
45 # Setup class variables...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
46 my($ClassName, $PathsPropertyName, $CyclicPathsPropertyName);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
47 _InitializeClass();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
48
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
49 # Overload Perl functions...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
50 use overload '""' => 'StringifyPathGraph';
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
51
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
52 # Class constructor...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
53 sub new {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
54 my($Class, $Graph) = @_;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
55
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
56 # Initialize object...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
57 my $This = $Class->SUPER::new();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
58 bless $This, ref($Class) || $Class;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
59 $This->_InitializePathGraph($Graph);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
60
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
61 $This->_ConvertGraphIntoPathGraph($Graph);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
62
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
63 return $This;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
64 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
65
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
66 # Initialize object data...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
67 sub _InitializePathGraph {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
68 my($This, $Graph) = @_;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
69
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
70 if (!(defined($Graph) && Graph::IsGraph($Graph))) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
71 croak "Error: ${ClassName}->new: PathGraph object can't be instantiated without a Graph object...";
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
72 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
73
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
74 $This->{Graph} = $Graph;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
75
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
76 # Maximum time allowed for cycles detection during collapse vertex cycles detection
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
77 # methodology in seconds...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
78 $This->{MaxAllowedTime} = 30;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
79
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
80 # Starting time for cycles detection during collapse vertex cycles detection
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
81 # methodology...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
82 $This->{StartTime} = time;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
83
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
84 return $This;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
85 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
86
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
87 # Initialize class ...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
88 sub _InitializeClass {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
89 #Class name...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
90 $ClassName = __PACKAGE__;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
91
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
92 # Path edge property name...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
93 $PathsPropertyName = 'Paths';
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
94
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
95 # Cyclic path vertex property name...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
96 $CyclicPathsPropertyName = 'CyclicPaths';
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
97 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
98
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
99 # Convert graph into a path graph...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
100 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
101 sub _ConvertGraphIntoPathGraph {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
102 my($This, $Graph) = @_;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
103
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
104 # Copy graph vertices and edges without any associated properties data
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
105 # from Graph to This: Graph properties data is available using Graph object reference
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
106 # store in This object...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
107 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
108 $Graph->CopyVerticesAndEdges($This);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
109
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
110 # . Attach Path property to each edge...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
111 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
112 my($Index, $VertexID1, $VertexID2, $Path, @EdgesVertexIDs);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
113
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
114 @EdgesVertexIDs = ();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
115 @EdgesVertexIDs = $This->GetEdges();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
116 for ($Index = 0; $Index < $#EdgesVertexIDs; $Index += 2) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
117 $VertexID1 = $EdgesVertexIDs[$Index]; $VertexID2 = $EdgesVertexIDs[$Index + 1];
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
118 $Path = new Graph::Path($VertexID1, $VertexID2);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
119 my(@Paths) = ();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
120 push @Paths, $Path;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
121 $This->SetEdgeProperty($PathsPropertyName, \@Paths, $VertexID1, $VertexID2);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
122 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
123 return $This;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
124 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
125
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
126 # Collapse paths around a specified vertex by updating paths around the vertex
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
127 # and adding any resulting cyclic paths to vertices attached to specified vertex.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
128 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
129 # Notes:
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
130 # . Path object references are stored as a list attached to Paths property on edges.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
131 # Usage of list allows multiple paths attached to the egde between a pair of vertices;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
132 # Graph doesn't support multiple egdes between a pair of vertices.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
133 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
134 # . Cyclic path object references are stored as list on vertices as CyclicPaths graph property.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
135 # List allows multiple Loop properties attached to a vertex.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
136 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
137 # . For topologically complex graphs containing large number of cycles, cycles detection algorithm
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
138 # [ Ref 31 ] as implemented implemented in CollapseVertexAndCollectCyclicPathsDetectCycles
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
139 # might not be able to find all the cycles in a reasonable amount of time and is designed to
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
140 # abandon cycles detection after MaxAllowedTime. Consequently, no cycles are detected
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
141 # or assigned.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
142 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
143 sub CollapseVertexAndCollectCyclicPaths {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
144 my($This, $VertexID) = @_;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
145
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
146 if (!$This->HasVertex($VertexID)) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
147 carp "Warning: ${ClassName}->CollapseVertexAndCollectCyclicPaths: Didn't collapse vertex $VertexID: Vertex $VertexID doesn't exist...";
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
148 return undef;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
149 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
150 # Collect all paths around specified VertexID by going over paths associated with its edges...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
151 my($Index, $EdgePathsRef, $EdgeVertexID1, $EdgeVertexID2, @Paths, @EdgesVertexIDs);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
152
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
153 @EdgesVertexIDs = ();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
154 @EdgesVertexIDs = $This->GetEdges($VertexID);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
155
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
156 @Paths = ();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
157 for ($Index = 0; $Index < $#EdgesVertexIDs; $Index += 2) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
158 ($EdgeVertexID1, $EdgeVertexID2) = ($EdgesVertexIDs[$Index], $EdgesVertexIDs[$Index + 1]);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
159 $EdgePathsRef = $This->GetEdgeProperty($PathsPropertyName, $EdgeVertexID1, $EdgeVertexID2);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
160 push @Paths, @{$EdgePathsRef};
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
161 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
162
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
163 # Go over each pair of paths around the specified vertex, join paths and associate
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
164 # joined path to appropriate edge...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
165 my($Index1, $Index2, $Path1, $Path2, $JoinedPath, $JoinedPathStartVertexID, $JoinedPathEndVertexID, @CommonVertices);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
166
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
167 for ($Index1 = 0; $Index1 < $#Paths; $Index1 +=1 ) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
168 $Path1 = $Paths[$Index1];
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
169
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
170 PATH2: for ($Index2 = $Index1 + 1; $Index2 <= $#Paths; $Index2 +=1 ) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
171 $Path2 = $Paths[$Index2];
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
172
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
173 # For JoinedPath to be valid cycle, Path1 and Path2 must have exactly two vertices in common.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
174 # Otherwise, joined path contains duplicate vertices besides the terminal vertices and
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
175 # indicates a path from a different direction.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
176 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
177 # For paths leading to cycles, it only makes sense to join paths with only one common vertex;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
178 # otherwise, it wouldn't lead to a cycle and can be ignored.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
179 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
180 @CommonVertices = $Path1->GetCommonVertices($Path2);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
181 if (!(@CommonVertices <= 2 && ($CommonVertices[0] == $VertexID || $CommonVertices[1] == $VertexID))) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
182 next PATH2;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
183 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
184
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
185 $JoinedPath = $Path1->JoinAtVertex($Path2, $VertexID);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
186 ($JoinedPathStartVertexID, $JoinedPathEndVertexID) = $JoinedPath->GetTerminalVertices();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
187
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
188 if (!$JoinedPath->IsIndependentPath()) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
189 next PATH2;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
190 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
191
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
192 # Decide whether to give up or keep going...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
193 if ($This->_IsTimeToGiveUpCyclesDetection()) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
194 warn "Warning: ${ClassName}->CollapseVertexAndCollectCyclicPaths: Cycles detection algorithm [ Ref 31 ] as implemented in the current release of MayaChemTools didn't finish with in the maximum allowed time of $This->{MaxAllowedTime} seconds; Cycles detection has been abandoned...";
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
195 return undef;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
196 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
197
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
198 if ($JoinedPathStartVertexID == $JoinedPathEndVertexID) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
199 # It's a cycle. Attach it to the graph as CylicPaths property...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
200 if ($This->HasGraphProperty($CyclicPathsPropertyName)) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
201 my($ExistingCyclicPathsRef);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
202 $ExistingCyclicPathsRef = $This->GetGraphProperty($CyclicPathsPropertyName);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
203 push @{$ExistingCyclicPathsRef}, $JoinedPath;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
204 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
205 else {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
206 my(@NewCyclicPaths) = ();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
207 push @NewCyclicPaths, $JoinedPath;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
208 $This->SetGraphProperty($CyclicPathsPropertyName, \@NewCyclicPaths, $JoinedPathStartVertexID);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
209 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
210 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
211 else {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
212 if ($This->HasEdge($JoinedPathStartVertexID, $JoinedPathEndVertexID)) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
213 # Append to the list of exisiting paths property of the edge...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
214 my($ExistingPathsRef);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
215 $ExistingPathsRef = $This->GetEdgeProperty($PathsPropertyName, $JoinedPathStartVertexID, $JoinedPathEndVertexID);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
216 push @{$ExistingPathsRef}, $JoinedPath;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
217 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
218 else {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
219 # Create a new edge and associate path property...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
220 my(@NewPaths) = ();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
221 push @NewPaths, $JoinedPath;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
222 $This->AddEdge($JoinedPathStartVertexID, $JoinedPathEndVertexID);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
223 $This->SetEdgeProperty($PathsPropertyName, \@NewPaths, $JoinedPathStartVertexID, $JoinedPathEndVertexID);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
224 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
225 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
226 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
227 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
228 $This->DeleteVertex($VertexID);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
229
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
230 return $This;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
231 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
232
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
233 # Decide whether to give up cycles detection using collapse vertex methodology...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
234 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
235 sub _IsTimeToGiveUpCyclesDetection {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
236 my($This) = @_;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
237
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
238 return ((time - $This->{StartTime}) > $This->{MaxAllowedTime}) ? 1 : 0;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
239 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
240
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
241 # Delete vertices with degree less than a specifed degree...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
242 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
243 sub DeleteVerticesWithDegreeLessThan {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
244 my($This, $Degree) = @_;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
245 my($VertexID, @VertexIDs);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
246
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
247 while (@VertexIDs = $This->GetVerticesWithDegreeLessThan($Degree)) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
248 for $VertexID (@VertexIDs) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
249 $This->DeleteVertex($VertexID);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
250 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
251 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
252 return $This;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
253 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
254
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
255 # Get paths associated with edges...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
256 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
257 sub GetPaths {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
258 my($This) = @_;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
259 my($PathsRef, @Paths, @PathsList);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
260
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
261 @Paths = (); @PathsList = ();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
262 @PathsList = $This->GetEdgesProperty($PathsPropertyName);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
263 for $PathsRef (@PathsList) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
264 push @Paths, @{$PathsRef};
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
265 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
266 return wantarray ? @Paths : scalar @Paths;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
267 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
268
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
269 # Get paths associated with edges which make a cylce...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
270 #
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
271 sub GetCyclicPaths {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
272 my($This) = @_;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
273 my($PathsRef, @Paths, @PathsList);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
274
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
275 @Paths = (); @PathsList = ();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
276 @PathsList = $This->GetGraphProperty($CyclicPathsPropertyName);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
277 PATHS: for $PathsRef (@PathsList) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
278 if (!(defined($PathsRef) && @{$PathsRef})) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
279 next PATHS;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
280 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
281 push @Paths, @{$PathsRef};
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
282 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
283 return wantarray ? @Paths : scalar @Paths;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
284 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
285
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
286 # Is it a path graph object?
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
287 sub IsPathGraph ($) {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
288 my($Object) = @_;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
289
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
290 return _IsPathGraph($Object);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
291 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
292
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
293 # Return a string containg data for PathGraph object...
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
294 sub StringifyPathGraph {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
295 my($This) = @_;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
296 my($PathGraphString);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
297
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
298 $PathGraphString = 'PathGraph:' . $This->StringifyVerticesAndEdges() . '; ' . $This->StringifyProperties();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
299
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
300 return $PathGraphString;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
301 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
302
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
303 # Is it a PathGraph object?
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
304 sub _IsPathGraph {
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
305 my($Object) = @_;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
306
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
307 return (Scalar::Util::blessed($Object) && $Object->isa($ClassName)) ? 1 : 0;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
308 }
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
309
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
310 1;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
311
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
312 __END__
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
313
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
314 =head1 NAME
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
315
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
316 PathGraph
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
317
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
318 =head1 SYNOPSIS
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
319
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
320 use Graph::PathGraph;
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
321
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
322 use Graph::PathGraph qw(:all);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
323
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
324 =head1 DESCRIPTION
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
325
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
326 B<PathGraph> class provides the following methods:
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
327
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
328 new, CollapseVertexAndCollectCyclicPaths, DeleteVerticesWithDegreeLessThan,
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
329 GetCyclicPaths, GetPaths, IsPathGraph, StringifyPathGraph
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
330
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
331 B<PathGraph> class is derived from I<Graph> class.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
332
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
333 =head2 METHODS
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
334
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
335 =over 4
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
336
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
337 =item B<new>
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
338
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
339 $NewPathGraph = new Graph::PathGraph($Graph);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
340
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
341 Using specified I<Graph>, B<new> method creates a new B<PathGraph> object and returns
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
342 newly created B<PathGraph> object.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
343
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
344 I<Graph> is converted into a B<PathGraph> by copying all its vertices and edges without any
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
345 associated properties data and associating a I<Path> object to each edge containing edge
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
346 vertex IDs as intial path.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
347
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
348 =item B<CollapseVertexAndCollectCyclicPaths>
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
349
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
350 $PathGraph->CollapseVertexAndCollectCyclicPaths($VertexID);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
351
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
352 Collapses paths around a I<VertexID> by updating paths around the vertex [Ref 31] and associating any
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
353 resulting cyclic paths to graph as B<CyclicPaths> property name. And returns I<PathGraph>.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
354
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
355 =item B<DeleteVerticesWithDegreeLessThan>
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
356
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
357 $Return = $PathGraph->DeleteVerticesWithDegreeLessThan($Degree);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
358
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
359 Deletes vertices with degree less than I<Degree> from I<PathGraph> and returns I<PathGraph>.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
360
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
361 =item B<GetCyclicPaths>
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
362
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
363 @CyclicPaths = $PathGraph->GetCyclicPaths();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
364 $NumOfPaths = $PathGraph->GetCyclicPaths();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
365
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
366 Returns an array of cyclic I<Paths> associated with edges in I<PathGraph>. In scalar context, number
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
367 of cyclic paths is returned.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
368
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
369 =item B<GetPaths>
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
370
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
371 @Paths = $PathGraph->GetPaths();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
372 $NumOfPaths = $PathGraph->GetPaths();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
373
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
374 Returns an array of I<Paths> associated with edges in I<PathGraph>. In scalar context, number
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
375 of paths is returned.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
376
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
377 =item B<IsPathGraph>
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
378
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
379 $Status = Graph::PathGraph::IsPathGraph($Object);
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
380
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
381 Returns 1 or 0 based on whether I<Object> is a B<PathGraph> object.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
382
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
383 =item B<StringifyPathGraph>
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
384
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
385 $String = $PathGraph->StringifyPathGraph();
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
386
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
387 Returns a string containing information about traversed paths in I<PathGraph> object.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
388
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
389 =back
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
390
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
391 =head1 AUTHOR
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
392
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
393 Manish Sud <msud@san.rr.com>
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
394
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
395 =head1 SEE ALSO
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
396
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
397 Graph.pm, Path.pm
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
398
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
399 =head1 COPYRIGHT
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
400
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
401 Copyright (C) 2015 Manish Sud. All rights reserved.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
402
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
403 This file is part of MayaChemTools.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
404
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
405 MayaChemTools is free software; you can redistribute it and/or modify it under
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
406 the terms of the GNU Lesser General Public License as published by the Free
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
407 Software Foundation; either version 3 of the License, or (at your option)
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
408 any later version.
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
409
68300206e90d Uploaded
deepakjadmin
parents:
diff changeset
410 =cut