27 #ifndef MERT_FST_LIB_SHORTEST_PATH_H__ 28 #define MERT_FST_LIB_SHORTEST_PATH_H__ 34 #include <fst/cache.h> 35 #include <fst/determinize.h> 36 #include <fst/queue.h> 37 #include <fst/shortest-distance.h> 38 #include <fst/test-properties.h> 53 template<
class FArc,
class TArc>
55 fst::MutableFst<TArc> *ofst,
F gamma) {
56 typedef typename FArc::StateId StateId;
57 typedef typename FArc::Weight FWeight;
58 typedef typename TArc::Weight Weight;
59 std::vector<Weight> distance;
60 fst::AnyArcFilter < TArc > arc_filter;
61 fst::AutoQueue<typename FArc::StateId> state_queue (*ofst, &distance,
64 ofst->SetInputSymbols (ifst.InputSymbols() );
65 ofst->SetOutputSymbols (ifst.OutputSymbols() );
66 if (ifst.Start() == fst::kNoStateId)
68 std::vector<Weight> rdistance;
69 std::vector<bool> enqueued;
70 std::vector<StateId> parent;
71 std::vector<TArc> arc_parent;
73 StateId source = ifst.Start();
74 Weight f_distance = Weight::Zero();
75 StateId f_parent = fst::kNoStateId;
78 while (distance.size() < source) {
79 distance.push_back (Weight::Zero() );
80 enqueued.push_back (
false);
81 parent.push_back (fst::kNoStateId);
82 arc_parent.push_back (TArc (fst::kNoLabel, fst::kNoLabel, Weight::Zero(),
85 distance.push_back (Weight::One() );
86 parent.push_back (fst::kNoStateId);
87 arc_parent.push_back (TArc (fst::kNoLabel, fst::kNoLabel, Weight::Zero(),
89 state_queue.Enqueue (source);
90 enqueued.push_back (
true);
91 while (!state_queue.Empty() ) {
92 StateId s = state_queue.Head();
93 state_queue.Dequeue();
95 Weight sd = distance[s];
96 if (ifst.Final (s) != FWeight::Zero() ) {
97 Weight w =
Times (sd, ifst.Final (s).Map (gamma) );
98 if (f_distance !=
Plus (f_distance, w) ) {
99 f_distance =
Plus (f_distance, w);
103 for (fst::ArcIterator < fst::Fst<FArc> > aiter (ifst, s); !aiter.Done();
105 const FArc& arc = aiter.Value();
106 while (distance.size() <= arc.nextstate) {
107 distance.push_back (Weight::Zero() );
108 enqueued.push_back (
false);
109 parent.push_back (fst::kNoStateId);
110 arc_parent.push_back (TArc (fst::kNoLabel, fst::kNoLabel, Weight::Zero(),
113 Weight& nd = distance[arc.nextstate];
114 Weight mw = arc.weight.Map (gamma);
115 Weight w =
Times (sd, mw);
116 if (nd !=
Plus (nd, w) ) {
118 parent[arc.nextstate] = s;
119 TArc tarc (arc.ilabel, arc.olabel, mw, arc.nextstate);
120 arc_parent[arc.nextstate] = tarc;
121 if (!enqueued[arc.nextstate]) {
122 state_queue.Enqueue (tarc.nextstate);
123 enqueued[arc.nextstate] =
true;
125 state_queue.Update (tarc.nextstate);
130 distance[source] = Weight::One();
131 parent[source] = fst::kNoStateId;
132 StateId s_p = fst::kNoStateId, d_p = fst::kNoStateId;
133 for (StateId s = f_parent, d = fst::kNoStateId; s != fst::kNoStateId; d = s, s
136 s_p = ofst->AddState();
137 if (d == fst::kNoStateId) {
138 ofst->SetFinal (s_p, ifst.Final (f_parent).Map (gamma) );
140 arc_parent[d].nextstate = d_p;
141 ofst->AddArc (s_p, arc_parent[d]);
144 ofst->SetStart (s_p);
149 #endif // MERT_FST_LIB_SHORTEST_PATH_H__ void SingleShortestPath(const fst::Fst< FArc > &ifst, fst::MutableFst< TArc > *ofst, F gamma)
TropicalSparseTupleWeight< T > Plus(const TropicalSparseTupleWeight< T > &vw1, const TropicalSparseTupleWeight< T > &vw2)
TropicalSparseTupleWeight< T > Times(const TropicalSparseTupleWeight< T > &w1, const TropicalSparseTupleWeight< T > &w2)