Presentation is loading. Please wait.

Presentation is loading. Please wait.

IUT de Reims-Châlons-Charleville rue des crayères, BP 1035 51687 Reims Cedex 2 Exemple de codage informatique des graphes Olivier Nocent

Similar presentations


Presentation on theme: "IUT de Reims-Châlons-Charleville rue des crayères, BP 1035 51687 Reims Cedex 2 Exemple de codage informatique des graphes Olivier Nocent"— Presentation transcript:

1 IUT de Reims-Châlons-Charleville rue des crayères, BP Reims Cedex 2 Exemple de codage informatique des graphes Olivier Nocent

2 IUT de Reims-Châlons-Charleville [ ]2 Diagramme de classes OrientedGraph Arc NodeSquareMatrix

3 IUT de Reims-Châlons-Charleville [ ]3 Classe Matrice (1,1)(1,2)(1,3) (2,1)(2,2)(2,3) (3,1)(3,2)(3,3) (1,1)(1,2)(1,3)(2,1)(2,2)(2,3)(3,1)(3,2)(3,3) Matrice = Tableau 2D de coefficients Vector = Tableau 1D de coefficients

4 IUT de Reims-Châlons-Charleville [ ]4 Classe Matrice class SquareMatrix : protected std::vector { public: SquareMatrix(unsigned int _n=0):std::vector (_n*_n, 0), n(_n) int& operator ()(int i, int j) { return (*this)[ (i-1)*n + j - 1 ]; } int operator ()(int i, int j) const { return (*this)[ (i-1)*n + j - 1 ]; } SquareMatrix operator +(const SquareMatrix& A) const { SquareMatrix B(n); std::transform(begin(), end(), A.begin(), B.begin(), std::plus ()); return B; } SquareMatrix operator *(const SquareMatrix& A) const; { SquareMatrix B(n); for (unsigned long i=1;i<=n;i++) for (unsigned long j=1;j<=n;j++) for (unsigned long k=1;k<=n;k++) B(i,j) += (*this)(i,k)*A(k,j); return B; } private: unsigned int n; };

5 IUT de Reims-Châlons-Charleville [ ]5 Classe Sommet class Node { public: Node(const std::string& _Label) :Label(_Label) std::string getLabel() const { return Label; } void addNextNode(Node* _NextNode) { NextNodes.push_back(_NextNode); } void addPreviousNode(Node* _NextNode) { PreviousNodes.push_back(_PreviousNode); } void explore(std::list & ReachableNodes) { // TO DO } friend class OrientedGraph; private: std::string Label; std::list PreviousNodes; std::list NextNodes; };

6 IUT de Reims-Châlons-Charleville [ ]6 Classe Arc class Arc { public: Arc(Node* _StartNode, Node* _EndNode, double _Weight=1.0) :StartNode(_StartNode), EndNode(_EndNode), Weight(_Weight) Node* getStartNode() const { return StartNode; } Node* getEndNode() const { return EndNode; } double getWeight() const { return Weight; } private: Node* StartNode; Node* EndNode; double Weight; };

7 IUT de Reims-Châlons-Charleville [ ]7 Classe Graphe Orienté class OrientedGraph { public: OrientedGraph():UpToDate(false) ~OrientedGraph() {} bool addNode(const std::string& NewNodeLabel); bool addArc(const std::string& StartNodeLabel, const std::string& EndNodeLabel, double Weight=1.0); Node* findNode(const std::string& NodeLabel); Arc* findArc(const std::string& StartNodeLabel, const std::string& EndNodeLabel); void computeTransitiveClosure() { // TO DO } void getReachableNodesFrom(const std::string& SourceNodeLabel, std::list & ReachableNodes) { // TO DO } bool isStronglyConnected() { // TO DO } void getMaximalStronglyConnectedComponentFrom(const std::string& SourceNodeLabel, std::list & ConnectedNodes) {//TO DO} private: std::list NodesList; std::list ArcsList; std::vector A; bool UpToDate; };

8 IUT de Reims-Châlons-Charleville [ ]8 Classe Graphe Orienté Node* OrientedGraph::findNode(const std::string& NodeLabel) { Node* ptr = NULL; bool Found = false; std::list ::iterator i = NodesList.begin(), e = NodesList.end(); while ( !Found && i!=e ) { if ( (*i)->getLabel() == NodeLabel ) { ptr = *i; Found = true; } i++; } return ptr; } Arc* OrientedGraph::findArc(const std::string& StartNodeLabel, const std::string& EndNodeLabel) { Arc* ptr = NULL; bool Found = false; std::list ::iterator i = ArcsList.begin(), e = ArcsList.end(); while ( !Found && i!=e ) { if ( ((*i)->getStartNode()->getLabel() == StartNodeLabel) && ((*i)->getEndNode()->getLabel() == EndNodeLabel) ) { ptr = *i;Found = true; } i++; } return ptr; }

9 IUT de Reims-Châlons-Charleville [ ]9 Classe Graphe Orienté bool OrientedGraph::addNode(const std::string& NewNodeLabel) { if (findNode(NewNodeLabel)) { std::cerr << NewNodeLabel << " : allready used !" << std::endl; return false; } NodesList.push_back(new Node(NewNodeLabel)); UpToDate = false; return true; } bool OrientedGraph::addArc(const std::string& StartNodeLabel, const std::string& EndNodeLabel, double Weight) { Node* StartNode = findNode(StartNodeLabel); if (StartNode) { Node* EndNode = findNode(EndNodeLabel); if (EndNode) { ArcsList.push_back(new Arc(StartNode, EndNode, Weight)); StartNode->addNextNode(EndNode); EndNode->addPreviousNode(StartNode); UpToDate = false; return true; } else { std::cerr << EndNodeLabel << " : not found !" << std::endl; return false; } else { std::cerr << StartNodeLabel << " : not found !" << std::endl; return false; }


Download ppt "IUT de Reims-Châlons-Charleville rue des crayères, BP 1035 51687 Reims Cedex 2 Exemple de codage informatique des graphes Olivier Nocent"

Similar presentations


Ads by Google