[Lemon-commits] [lemon_svn] athos: r350 - hugo/trunk/src/work/athos

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:38:57 CET 2006


Author: athos
Date: Fri Mar 26 15:59:18 2004
New Revision: 350

Added:
   hugo/trunk/src/work/athos/dijkstra_at.h
   hugo/trunk/src/work/athos/dijkstra_demo.cc
   hugo/trunk/src/work/athos/kruskal.h
   hugo/trunk/src/work/athos/uf_demo.cc
   hugo/trunk/src/work/athos/union_find.h

Log:
Hozzáadtam pár dolgot, mielőtt áttérünk az svn-re.


Added: hugo/trunk/src/work/athos/dijkstra_at.h
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/athos/dijkstra_at.h	Fri Mar 26 15:59:18 2004
@@ -0,0 +1,123 @@
+/*
+Egy pontból összes többibe vezetõ legrövidebb utak irányított gráfban
+
+Preconditions:
+A gráf típus tud:
+- pontból kimenõ éleket sorban visszaadni
+A T számtípus, ami tud összehasonlítást, összedást
+A bemenetre:
+weight nemnegatív
+
+*/
+#ifndef DIJKSTRA_AT_H
+#define DIJKSTRA_AT_H
+
+
+
+//#include <algorithm>
+//#include <deque>
+//#include <vector>
+//#include "pf_hiba.hh"
+//#include <marci_list_graph.hh>
+//#include <marci_graph_traits.hh>
+
+
+using namespace std;
+
+namespace hugo {
+
+  template <typename graph_type, typename T>
+  class dijkstra_at {
+
+    //Hasznos typedef-ek
+    typedef typename graph_type::NodeIt NodeIt;
+    typedef typename graph_type::EdgeIt EdgeIt;
+    typedef typename graph_type::EachNodeIt EachNodeIt;
+    typedef typename graph_type::EachEdgeIt EachEdgeIt;
+    typedef typename graph_type::OutEdgeIt OutEdgeIt;
+    typedef typename graph_type::InEdgeIt InEdgeIt;
+    typedef typename graph_type::SymEdgeIt SymEdgeIt;
+
+
+
+    //---------------------------------------------
+    //Parameters of the algorithm
+    //---------------------------------------------
+
+    //---------------------------------------------
+    //Parameters of the algorithm
+    //---------------------------------------------
+ 
+  private:
+    //input
+    graph_type& G;
+    NodeIt s;
+    typename graph_type::EdgeMap<T> &weight;
+    //typename graph_type::EdgeMap<T>  &capacity;
+    //output
+    //typename graph_type::EdgeMap<T>  
+    //    typename graph_type::EdgeMap<T> preflow;
+      
+    //auxiliary variables for computation
+    deque<NodeIt> next_to_reach;
+    
+    
+    typename graph_type::NodeMap<bool> reached;
+
+    //Variables holding output
+    //Predessors in the shortest paths arborescence
+    typename graph_type::NodeMap<NodeIt> pred;
+
+
+  public:
+  
+
+    dijkstra_at(
+		      graph_type& _G, 
+		      NodeIt _s, 
+		      typename graph_type::EdgeMap<T> & _weight)
+      : G(_G), s(_s),
+      weight(_weight),
+      next_to_reach(),
+      reached(_G),
+      pred(G)
+     	
+    { 
+    }
+      /*By Misi.*/
+      struct Node_dist_comp
+      {
+	NodeMap<graph_type, T> &d;
+	Node_dist_comp(NodeMap<graph_type, T> &_d) : d(_d) {} 
+	
+	bool operator()(const NodeIt& u, const NodeIt& v) const 
+	{ return d.get(u) < d.get(v); }
+      };
+
+
+      
+      void run() {
+	
+	NodeMap<graph_type, bool> scanned(G, false);
+	std::priority_queue<NodeIt, vector<NodeIt>, Node_dist_comp> 
+	  heap(( Node_dist_comp(distance) ));
+	
+	heap.push(s);
+	reached.set(s, true);
+	
+      }
+
+      
+    };
+  
+
+
+
+
+ 
+  };  //class dijkstra_at  
+
+
+}//namespace hugo
+
+#endif //DIJKSTRA_AT

Added: hugo/trunk/src/work/athos/dijkstra_demo.cc
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/athos/dijkstra_demo.cc	Fri Mar 26 15:59:18 2004
@@ -0,0 +1,160 @@
+#include <iostream>
+#include <string>
+
+#include "list_graph.hh"
+//#include "marci_property_vector.hh"
+#include <dijkstra_at.h>
+
+using namespace hugo;
+
+
+int main (int, char*[])
+{
+
+  
+  typedef ListGraph::NodeIt NodeIt;
+  typedef ListGraph::EdgeIt EdgeIt;
+  /*
+  typedef ListGraph::EachNodeIt EachNodeIt;
+  typedef ListGraph::EachEdgeIt EachEdgeIt;
+  typedef ListGraph::OutEdgeIt OutEdgeIt;
+  typedef ListGraph::InEdgeIt InEdgeIt;
+  typedef ListGraph::SymEdgeIt SymEdgeIt;
+  */
+  ListGraph flowG;
+
+  /*
+  //Marci példája
+
+
+  NodeIt s=flowG.addNode();
+  NodeIt v1=flowG.addNode();
+  NodeIt v2=flowG.addNode();
+  NodeIt v3=flowG.addNode();
+  NodeIt v4=flowG.addNode();
+  NodeIt t=flowG.addNode();
+  
+
+  EdgeIt s_v1=flowG.addEdge(s, v1);
+  EdgeIt s_v2=flowG.addEdge(s, v2);
+  EdgeIt v1_v2=flowG.addEdge(v1, v2);
+  EdgeIt v2_v1=flowG.addEdge(v2, v1);
+  EdgeIt v1_v3=flowG.addEdge(v1, v3);
+  EdgeIt v3_v2=flowG.addEdge(v3, v2);
+  EdgeIt v2_v4=flowG.addEdge(v2, v4);
+  EdgeIt v4_v3=flowG.addEdge(v4, v3);
+  EdgeIt v3_t=flowG.addEdge(v3, t);
+  EdgeIt v4_t=flowG.addEdge(v4, t);
+
+  ListGraph::EdgeMap<int> cap(flowG);
+
+  cap.set(s_v1, 16);
+  cap.set(s_v2, 13);
+  cap.set(v1_v2, 10);
+  cap.set(v2_v1, 4);
+  cap.set(v1_v3, 12);
+  cap.set(v3_v2, 9);
+  cap.set(v2_v4, 14);
+  cap.set(v4_v3, 7);
+  cap.set(v3_t, 20);
+  cap.set(v4_t, 4);
+  */
+
+
+  //Ahuja könyv példája
+
+  NodeIt s=flowG.addNode();
+  NodeIt v2=flowG.addNode();
+  NodeIt v3=flowG.addNode();
+  NodeIt v4=flowG.addNode();
+  NodeIt v5=flowG.addNode();
+  NodeIt t=flowG.addNode();
+
+  EdgeIt s_v2=flowG.addEdge(s, v2);
+  EdgeIt s_v3=flowG.addEdge(s, v3);
+  EdgeIt v2_v4=flowG.addEdge(v2, v4);
+  EdgeIt v2_v5=flowG.addEdge(v2, v5);
+  EdgeIt v3_v5=flowG.addEdge(v3, v5);
+  EdgeIt v4_t=flowG.addEdge(v4, t);
+  EdgeIt v5_t=flowG.addEdge(v5, t);
+  
+  //Kis modositas
+  //edge_iterator v2_s=flowG.add_edge(v2, s);
+
+  ListGraph::EdgeMap<int> cap(flowG);
+
+  cap.set(s_v2, 10);
+  cap.set(s_v3, 10);
+  cap.set(v2_v4, 5);
+  cap.set(v2_v5, 8);
+  cap.set(v3_v5, 5);
+  cap.set(v4_t, 8);
+  cap.set(v5_t, 8);
+
+  //Kis modositas
+  //cap.put(v2_s, 100);
+ 
+
+
+
+  /*Egyszerû példa
+  NodeIt s=flow_test.add_node();
+  NodeIt v1=flow_test.add_node();
+  NodeIt v2=flow_test.add_node();
+  NodeIt t=flow_test.add_node();
+  
+  node_property_vector<list_graph, std::string> node_name(flow_test);
+  node_name.put(s, "s");
+  node_name.put(v1, "v1");
+  node_name.put(v2, "v2");
+  node_name.put(t, "t");
+
+  edge_iterator s_v1=flow_test.add_edge(s, v1);
+  edge_iterator v1_v2=flow_test.add_edge(v1, v2);
+  edge_iterator v2_t=flow_test.add_edge(v2, t);
+
+  edge_property_vector<list_graph, int> cap(flow_test); 
+    
+  cap.put(s_v1, 16);
+  cap.put(v1_v2, 10);
+  cap.put(v2_t, 4);
+  */
+
+  std::cout << "preflow-push algorithm test..." << std::endl;
+
+  /*
+  std::cout << "on directed graph graph" << std::endl; //<< flow_test;
+  std::cout << "names and capacity values" << std::endl; 
+  for(EachNodeIt i=flow_test.first_node(); i.valid(); ++i) { 
+    std::cout << node_name.get(i) << ": ";
+    std::cout << "out edges: ";
+    for(out_edge_iterator j=flow_test.first_out_edge(i); j.valid(); ++j) 
+      std::cout << node_name.get(flow_test.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flow_test.head(j)) << " ";
+    std::cout << "in edges: ";
+    for(in_edge_iterator j=flow_test.first_in_edge(i); j.valid(); ++j) 
+      std::cout << node_name.get(flow_test.tail(j)) << "-"<< cap.get(j) << "->" << node_name.get(flow_test.head(j)) << " ";
+    std::cout << std::endl;
+  }
+  */
+  
+  //for(each_NodeIt i=flow_test.first_node(); i.valid(); ++i) { 
+  //  std::cout << i << " ";
+  //}
+  
+  dijkstra_at<ListGraph, int> dijkstra_test(flowG, s, cap);
+  //cout << preflow_push_test.run()<<endl;
+
+  //cap.put(v5_t, 9);
+  //cout << preflow_push_test.run()<<endl;
+
+  return 0;
+}
+
+
+
+
+
+
+
+
+

Added: hugo/trunk/src/work/athos/kruskal.h
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/athos/kruskal.h	Fri Mar 26 15:59:18 2004
@@ -0,0 +1,61 @@
+/*
+Kruskal's algorithm to find a spanning tree of minimum weight
+If the graph is not connected, it gives a forest. 
+*/
+#ifndef KRUSKAL_H
+#define KRUSKAL_H
+
+#include <union_find.h>
+
+namespace hugo {
+  
+  template <typename graph_type, typename T>
+    class Kruskal {
+
+    
+    //Hasznos typedef-ek
+    typedef typename graph_type::NodeIt NodeIt;
+    typedef typename graph_type::EdgeIt EdgeIt;
+    typedef typename graph_type::EachNodeIt EachNodeIt;
+    typedef typename graph_type::EachEdgeIt EachEdgeIt;
+    typedef typename graph_type::SymEdgeIt SymEdgeIt;
+
+    //input
+    graph_type& G;
+    typename graph_type::EdgeMap<T> &weight;
+
+    //Auxilliary variables
+    typename graph_type::NodeMap<int> component(flowG);
+
+    Kruskal(
+	    graph_type& _G, 
+	    typename graph_type::EdgeMap<T> & _weight)
+      : G(_G),
+      weight(_weight),
+      component(-1)
+      { 
+      }
+
+      /*Originally by Misi.*/
+      struct Edge_comp
+      {
+	NodeMap<graph_type, T> &d;
+	Node_dist_comp(NodeMap<graph_type, T> &_d) : d(_d) {} 
+	
+	bool operator()(const NodeIt& u, const NodeIt& v) const 
+	{ return d.get(u) < d.get(v); }
+      };
+
+
+      //Runs the algorithm
+      void run() {
+	
+
+	
+      }
+     
+  }
+
+}//namespacc hugo
+
+#endif

Added: hugo/trunk/src/work/athos/uf_demo.cc
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/athos/uf_demo.cc	Fri Mar 26 15:59:18 2004
@@ -0,0 +1,48 @@
+//This is just a simple example program to test my union-find structure
+
+//#include <marciMap.hh>
+#include <union_find.h>
+#include <iostream>
+#include <list_graph.hh>
+using namespace hugo;
+using namespace std;
+
+int main (int, char*[])
+{
+  typedef ListGraph::NodeIt NodeIt;
+  typedef ListGraph::EachNodeIt EachNodeIt;
+  typedef ListGraph::EdgeIt EdgeIt;
+
+  ListGraph flowG;
+
+  
+  //Marci példája
+
+
+
+  NodeIt s=flowG.addNode();
+  NodeIt v1=flowG.addNode();
+  NodeIt v2=flowG.addNode();
+  NodeIt v3=flowG.addNode();
+  NodeIt v4=flowG.addNode();
+  NodeIt t=flowG.addNode();
+
+  ListGraph::NodeMap<int> component(flowG);
+
+  component.set(s, -1);
+  component.set(v1, -1);
+  component.set(v2, -1);
+  component.set(v3, -1);
+  component.set(v4, -1);
+  component.set(t, -1);
+
+  UnionFind< NodeIt, ListGraph::NodeMap<int> > uf(component);
+  cout<<"Merge s and v1: "<<uf.findAndMerge(s,v1)<<endl;
+  cout<<"Merge s and v1: "<<uf.findAndMerge(s,v1)<<endl;
+  cout<<"Merge s and v2: "<<uf.findAndMerge(s,v3)<<endl;
+  for(EachNodeIt i=flowG.template first<EachNodeIt>(); i.valid(); ++i) {
+
+    cout<<"Az "<<flowG.id(i)<<". pont itt van: "<<uf.find(i)<<endl;
+    //std::cout << node_name.get(i) << ": ";
+  }
+}

Added: hugo/trunk/src/work/athos/union_find.h
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/athos/union_find.h	Fri Mar 26 15:59:18 2004
@@ -0,0 +1,94 @@
+// -*- C++ -*- //
+/*
+Union-Find structure
+
+* Ehhez nagyon fontos, hogy az atadott property map inicializalva legyen
+* minden szobajovo kulcs ertekre, -1 -es ertekkel!
+ 
+*/
+#ifndef UNION_FIND_H
+#define UNION_FIND_H
+
+#include <vector>
+//#include <map>
+
+
+namespace hugo {
+  
+  template <typename KeyType, typename KeyIntMap>
+    class UnionFind {
+    KeyIntMap& pointmap;
+    struct VectorElementType {
+      int boss;
+      int count;
+      VectorElementType(int _boss, int _count){
+	boss = _boss;
+	count = _count;
+      }
+    };
+    std::vector<VectorElementType> container;
+    public:
+      
+    UnionFind(KeyIntMap& _pointmap): pointmap(_pointmap){
+      
+    } 
+    
+    //Give a component of one point to the structure
+    int addPoint(KeyType u){
+      int _index = container.size();
+      VectorElementType buf(_index,1);
+      container.push_back(buf);
+      return _index;
+    }
+
+    
+    //Finds the big boss of u
+    int find(KeyType u){
+      if (pointmap.get(u)==-1){
+	int whoami = addPoint(u);
+	pointmap.set(u, whoami);
+	return whoami;
+      }
+      else{
+	int emp = pointmap.get(u);
+	int boss = container[emp].boss;
+	while(emp != boss){
+	  emp = boss;
+	  boss = container[emp].boss;
+	}
+	return boss;
+      }
+    }
+
+    //Finds u and v in the structures and merges the comopnents, if not equal
+    bool findAndMerge(KeyType u,KeyType v){
+      int bu = find(u);
+      int bv = find(v);
+      if (bu != bv){
+	unio(bu,bv);
+	return true;
+      }
+      else{
+	return false;
+      }
+    }
+
+    //Merges a into b
+    void mergeInto(int a, int b){
+      container[a].boss = b;
+      container[b].count +=  container[a].count;
+    }
+    
+    //Makes the union
+    void unio(int b1, int b2){
+      if (container[b1].count>container[b2].count){
+	mergeInto(b2,b1);
+      }
+      else{
+	mergeInto(b1,b2);
+      }
+    }//unio
+  };
+  
+}//namespace hugo
+#endif



More information about the Lemon-commits mailing list