Anda di halaman 1dari 12

***grafo extra***

#ifndef GRAPHTYPE_H
#define GRAPHTYPE_H
#include<vector>
#include<iostream>
using namespace std;
#define
#define
#define
#define

UNEXPLORER 0
VISITED 1
DISCOVERED 2
BACK 3

template <class E> class graph;


template <class E> class edge;
template <class E> class vertex
{
private:
E data;
int refSecVertex;
vector<edge<E>*> *secIncident;
int label;
public:
vertex(E,int);
friend ostream& operator << <>(ostream&,vertex<E>&);
friend class graph<E>;
friend class edge<E>;
};
template <class E> vertex<E>::vertex(E data,int pos)
{
this->data=data;
this->refSecVertex=pos;
this->secIncident=new vector<edge<E>*>;
this->label=UNEXPLORER;
}
template <class E> ostream& operator << <>(ostream &o,vertex<E> &v)
{
o<<v.data<<"->";
return o;
}
template <class E> class edge
{
private:
vertex<E>* refVertexOrig;
vertex<E>* refVertexDest;

int refSecEdge;
vector<edge<E>*> *refSecIncidentDest;
vector<edge<E>*> *refSecIncidentOrig;
int label;
public:
edge(vertex<E>*,vertex<E>*,int);
friend class graph<E>;
};
template <class E> edge<E>::edge(vertex<E>* vertexOrig, vertex<E>*
vertexDest,int pos)
{
this->refVertexOrig=vertexOrig;
this->refVertexDest=vertexDest;
this->refSecEdge=pos;
this->refSecIncidentOrig=vertexOrig->secIncident;
this->refSecIncidentDest=vertexDest->secIncident;
this->label=UNEXPLORER;
}
template <class E> class graph
{
private:
vector<vertex<E>*> *secVertex;
vector<edge<E>*> *secEdge;
public:
graph();
//------------------Update Methods-----------------------void insertVertex(E);
void insertEdge(E,E);
vertex<E>* findVertex(E);
edge<E>* findEdge(vertex<E>* , vertex<E>* );
void DFS();
void BFS();
void pathDFS(E, E);
private:
void setLabelVertex();
void setLabelEdge();
void DFS(vertex<E>*);
void BFS(vertex<E>*);
void pathDFS(vertex<E>* , vertex<E>*);
vertex<E>* opposite(vertex<E>* , edge<E>*);
};
template <class E> graph<E>::graph()
{
this->secVertex=new vector<vertex<E>*>;
this->secEdge=new vector<edge<E>*>;}

template <class E> void graph<E>::insertVertex(E item)


{
vertex<E>*tempVertex = findVertex(item);
if(tempVertex) return;
int pos=this->secVertex->size();
vertex<E> *newVertex=new vertex<E>(item,pos);
this->secVertex->push_back(newVertex);}
template <class E> vertex<E>* graph<E>::findVertex(E item)
{
for(int i=0; i<secVertex->size(); i++)
if(secVertex->at(i)->data == item)
return secVertex->at(i);
return 0;
}
template <class E> void graph<E>::insertEdge(E vertexOrig,E vertexDest)
{
vertex<E>* refVertexOrig=findVertex(vertexOrig);
vertex<E>* refVertexDest=findVertex(vertexDest);
if(!refVertexOrig || !refVertexDest) return;
edge<E>*refEdge=findEdge(refVertexOrig,refVertexDest);
if(refEdge) return;
edge<E>*newEdge=new edge<E>(refVertexOrig,refVertexDest,
secEdge->size());
secEdge->push_back(newEdge);
refVertexOrig->secIncident->push_back(newEdge);
refVertexDest->secIncident->push_back(newEdge);
}
template <class E> edge<E>* graph<E>::findEdge(vertex<E>* vertexOrig,
vertex<E>* vertexDest){
for(int i=0; i<secEdge->size(); i++)
if((secEdge->at(i)->refVertexOrig == vertexOrig || secEdge->at(i)>refVertexDest == vertexOrig)
&& (secEdge->at(i)->refVertexOrig == vertexDest ||
secEdge->at(i)->refVertexDest == vertexDest))
return secEdge->at(i);
return 0;
}
template <class E> void graph<E>::DFS()
{
setLabelVertex();
setLabelEdge();
cout<<"Inicio: ";
for(int i=0; i<secVertex->size(); i++)
if(secVertex->at(i)->label == UNEXPLORER)
DFS(secVertex->at(i));}
template <class E> void graph<E>::setLabelVertex()
{
for(int i=0; i<secVertex->size(); i++)
secVertex->at(i)->label=UNEXPLORER;}
template <class E> void graph<E>::setLabelEdge()
{
for(int i=0; i<secEdge->size(); i++)
secEdge->at(i)->label=UNEXPLORER;}

template <class E> void graph<E>::DFS(vertex<E>* beginVertex)


{
vertex<E>* w;
beginVertex->label=VISITED;
cout<<*beginVertex;
for(int i=0; i<beginVertex->secIncident->size(); i++)
if (beginVertex->secIncident->at(i)->label == UNEXPLORER)
{
w=opposite(beginVertex,beginVertex->secIncident>at(i));
if(w->label == UNEXPLORER)
{
beginVertex->secIncident->at(i)>label=DISCOVERED;
DFS(w);
}
else
beginVertex->secIncident->at(i)->label=BACK;
}
template <class E> vertex<E>* graph<E>::opposite(vertex<E>*
beginVertex, edge<E>*e){
if(e->refVertexOrig == beginVertex)
return e->refVertexDest;
else
return e->refVertexOrig;
}
template <class E> void graph<E>::pathDFS(E item1, E item2)
{
vertex<E>* tempVertex1 = findVertex(item1);
vertex<E>* tempVertex2 = findVertex(item2);
if(!tempVertex1 || !tempVertex2) return;
setLabelVertex();
setLabelEdge();
cout<<"Inicio: ";
pathDFS(tempVertex1, tempVertex2);
}
template <class E> void graph<E>::pathDFS(vertex<E>* beginVertex,
vertex<E>* current)
{
vector<vertex<E>*>* list = new vector<vertex<E>*>;
vertex<E>* w;
beginVertex->label=VISITED;
list->push_back(beginVertex);
if (beginVertex == current)
{
for (int i = 0; i < list->size(); i++)
cout<<*(list->at(i));
return;
}
for(int i=0; i<beginVertex->secIncident->size(); i++)
{
if (beginVertex->secIncident->at(i)->label == UNEXPLORER)
{
w=opposite(beginVertex,beginVertex->secIncident>at(i));

if(w->label == UNEXPLORER)
{beginVertex->secIncident->at(i)->label=DISCOVERED;
//list->push_back(beginVertex->secIncident>at(i)); Para un vector que acepte nodos y aristas.
pathDFS(w,current);
//list->pop_back();
}
else
beginVertex->secIncident->at(i)->label=BACK;}}
list->pop_back();}
template <class E> void graph<E>::BFS()
{setLabelVertex();
setLabelEdge();
cout<<"Inicio: ";
for(int i=0; i<secVertex->size(); i++)
if(secVertex->at(i)->label == UNEXPLORER)
BFS(secVertex->at(i));}
template <class E> void graph<E>::BFS(vertex<E>* beginVertex)
{
vector<vertex<E>*>* list = new vector<vertex<E>*>;
vertex<E>* w;
beginVertex->label=VISITED;
list->push_back(beginVertex);
int i=0;
while (list->at(i) == NULL)
{for (int j = 0; j < list->size(); j++)
{for(int k = 0; k < beginVertex->secIncident->size(); k++)
{if (beginVertex->secIncident->at(i)->label ==
UNEXPLORER)
{w=opposite(beginVertex,beginVertex>secIncident->at(i));
if(w->label == UNEXPLORER)
{beginVertex->secIncident->at(i)>label=DISCOVERED;
w->label=VISITED;
list->push_back(w);
}
else
beginVertex->secIncident->at(i)>label=BACK;}}
i++;
}
for (int j = 0; j < list->size(); j++)
cout<<*(list->at(i));}
#endif*****grafo adyacente*****
#ifndef GRAPHMA_H
#define GRAPHMA_H
#include <iostream>
using namespace std;

#define N 10
//cantidad maxima de vertices
template<class E>class GraphMA{
private:
int **matrixAd;
//matriz de adyacencia
E *lstVertex;
//lista de vertices. La posicion ocupada determina
la fila y columna que
//representa a un vertice en la matriz
int nVertex;
//cantidad de v?rtices en un momento
determinado
public:
GraphMA();
void insertVertex(E);
int findVertex(E);
void insertEdge(E,E);
};
template<class E>GraphMA<E>::GraphMA(){
int i,j;
matrixAd = new int *[N];
for(i = 0; i<N; i++)
matrixAd[i] = new int[N];
for(i = 0; i<N; i++)
for(j = 0; j<N; j++)
matrixAd[i][j] = 0;
lstVertex = new E[N];
nVertex = 0;
}
template<class E>void GraphMA<E>::insertVertex(E item){
if (nVertex >= N){
cout<<"Espacio insuficiente para seguir insertando....\n";
return;
}
int pos = findVertex(item);
if (pos!=-1){
cout<<"Vertice duplicado...\n";
return;
}
lstVertex[nVertex++] = item;
}
template<class E>int GraphMA<E>::findVertex(E item){
//se deja para su implementacion
}
template<class E>void GraphMA<E>::insertEdge(E vOrig, E vDest){
int posOrig = findVertex(vOrig);
int posDest = findVertex(vDest);
if (posOrig == -1 or posDest == -1){
cout<<"Alguno de los vertices extremos no existe....\n";
return;

}
int posEdge = findEdge(posOrig, posDest);
if(posEdge != 0){
cout<<"Arista duplicada....\n";
return;
}
matrixAd[posOrig][posDest] = matrixAd[posDest][posOrig] = 1;
}
**** btreee
#ifndef BTREE_H
#define BTREE_H
#include "nodeb.h"
template<class E> class Btree{
private:
NodeB<E>* root;
int orden;
void push(NodeB<E>*, E, bool*, E*, NodeB<E>**);
void putNode(NodeB<E>*, E, NodeB<E>*, int);
void removeRegister(NodeB<E>*, E, bool*);
void removeKey(NodeB<E>*, int);
void restore(NodeB<E>*, int);
bool findNode(NodeB<E>*, E, int *);
void dividedNode(NodeB<E>*, E, NodeB<E>*, int, E*, NodeB<E>**);
friend ostream& operator << <>(ostream&, Btree<E>&);
void showTree(NodeB<E>*);
void sucesorFind(NodeB<E>*, int);
public:
Btree(int);
void insert(E);
void remove(E);
};
template<class E> Btree<E>::Btree(int orden){
this->orden = orden;
root = 0;
}
template<class E> void Btree<E>::insert(E ele){
bool up;
E clMediana;
NodeB<E> *nd;
NodeB<E> *pnew;
push(root, ele, &up, &clMediana, &nd);
if (up){
pnew = new NodeB<E>(orden);
pnew->getKey()[0] = clMediana;
pnew->getChilds()[0] = root;
pnew->getChilds()[1] = nd;
pnew->setCount(1);
root = pnew;

}
//cout << "Nodo: " << *root ;
}
template<class E> void Btree<E>::remove(E key){
bool found;
removeRegister(root, key, &found);
if (found){
if (root->getCount() == 0){
NodeB<E>*p = root;
root = root->getChilds()[0];
delete p;
}
}
else
cout << "El nodo no existe..." << endl;
}
template<class E> void Btree<E>::removeRegister(NodeB<E> *current, E
key, bool *found){
int pos;
if (current) {
(*found) = findNode(current, key, &pos);
if (*found) {
if (!current->getChilds()[0]) //hoja
removeKey(current, pos);
else {
sucesorFind(current, pos);
removeRegister(current->getChilds()[pos + 1],
current->getKey()[pos], found);
}
}
else
removeRegister(current->getChilds()[pos], key, found);
if (current->getChilds()[pos])
{
if (current->getChilds()[pos]->nodeEmpty(orden - 1))
restore(current, pos);
}
}
else
*found = false;
}
template<class E> void Btree<E>::removeKey(NodeB<E> *a, int elem){
int i;
for (i = elem + 1; i<a->getCount(); i++)
{
a->getKey()[i - 1] = a->getKey()[i];
a->getChilds()[i - 1] = a->getChilds()[i];
}
a->getChilds()[i - 1] = a->getChilds()[i];
a->count--;
}

template<class E> void Btree<E>::push(NodeB<E>* act, E ele, bool *up, E*


clMediana, NodeB<E>** nd){
int pos;
if (!act){
(*up) = true;
(*clMediana) = ele ;
(*nd) = 0;
}
else {
bool fl = findNode(act, ele, &pos);
if (fl){
cout << "Elemento ya existe...";
(*up) = false;
return;
}
push(act->getChilds()[pos], ele, up, clMediana, nd);
if (up){
if (act->nodeFull(orden - 1)){
//dividir
//cout << "Dividir...." << endl;
//(*up) = false;
dividedNode(act, *clMediana, *nd, pos, clMediana,
nd);
}
else{
(*up) = false;
putNode(act, *clMediana, *nd, pos);
template<class E> bool Btree<E>::findNode(NodeB<E>* act, E ele, int
*pos){
(*pos) = 0;
while ((*pos) < act->getCount() && ele > act->getKey()[(*pos)])
(*pos)++;
return (ele == act->getKey()[(*pos)]);
}
template<class E> void Btree<E>::putNode(NodeB<E>* act, E
clMediana,NodeB<E> *nd, int pos){
int i;
for (i = act->getCount() - 1; i >= pos; i--){
act->getKey()[i + 1] = act->getKey()[i];
act->getChilds()[i + 2] = act->getChilds()[i+1];
}
act->getKey()[i + 1]=clMediana;
act->getChilds()[i + 2] = nd;
act->setCount(act->getCount()+1);
}
template <class E> ostream& operator << <>(ostream &os, Btree<E> &b)
{
if (b.root){

b.showTree(b.root);
}
return os;
}
template <class E> void Btree<E>::showTree(NodeB<E> *act){
if (act){
cout << *act << endl;
for (int i = 0; i <= act->getCount(); i++){
showTree(act->getChilds()[i]);
}
}
}
template <class E> void Btree<E>::dividedNode(NodeB<E>* act, E cl,
NodeB<E>* hd, int pos, E* clMediana, NodeB<E>**nd){
int i, posMdna;
posMdna = (pos <= orden/2) ? orden / 2 : orden / 2 + 1;
(*nd) = new NodeB<E>(orden);
for (i = posMdna; i < orden - 1; i++) {
(*nd)->getKey()[i - posMdna] = act->getKey()[i];
(*nd)->getChilds()[i - posMdna + 1] = act->getChilds()[i + 1];
}
(*nd)->setCount(orden - 1 - posMdna);
act->setCount(posMdna);
if (pos <= orden / 2)
putNode(act, cl, hd, pos);
else
putNode(*nd, cl, hd, pos - posMdna);
(*clMediana) = act->getKey()[act->getCount() + 1];
(*nd)->getChilds()[0] = act->getChilds()[act->getCount()];
act->setCount(act->getCount() + 1);
}
template <class E> void Btree<E>::sucesorFind(NodeB<E> *a, int elem){
NodeB<E> *nodeKey = a;
a = a->getChilds()[elem + 1];
while (a->getChilds()[0])
a = a->getChilds()[0];
nodeKey->getKey()[elem] = a->getKey()[0];
}
#endif
template <class E>void insertar(NodeB<E>& nd, E ele, int max){
if (nd.getCount() >= max)
cout << "overflow..." << endl;
else
{
int i;
for (i = nd.getCount() - 1; i >= 0 && nd[i] > ele; i--)
nd[i + 1] = nd[i];
nd[i + 1] = ele;

nd.setCount(nd.getCount() + 1);
}
}
****bsqueda sucesor***
template<class E>
void Btree<E>::buscarSucesor() {
buscarSucesor(root, 0);
}
template<class E>
NodeB<E>* Btree<E>::buscarSucesor(NodeB<E>* act, int elem) {
cout << act->getCount() << endl;
cout << *act->getChilds()[0] << endl;
int cap = act->getCount();
if (act = NULL)
return NULL;
else {
for (int i = 0; i < cap; i++) {
cout << *act->getChilds()[i] << "**";}}}
*****Busqueda en profundidad****
template<class E>void CGrafo<E>::DFS(E vert)
{
cout<<"Recorrido DFS:"<<endl;
CVertice<E>*ptrVert=findVertice(vert);
if(ptrVert)
{
for(int i=0;i<SecVertice->size();i++)
SecVertice->at(i)->setLabel(UNEXPLORED);
DFS(ptrVert);
cout<<endl;
}else
cout<<"No existe este vrtice"<<endl;
}
//Mtodo soporte - recursivo para realizar la bsqueda
template<class E>void CGrafo<E>::DFS(CVertice<E>* ptrVert)
{cout<<ptrVert->getData()<<" ";
ptrVert->setLabel(VISITED);
if(!ptrVert->getSecIncident()->empty())
{for(int i=0;i<ptrVert->getSecIncident()->size();i++)
if(ptrVert->getSecIncident()->at(i)->getRefDestino()>getLabel()==UNEXPLORED)
DFS(ptrVert->getSecIncident()->at(i)->getRefDestino());
***lista enlazada
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include<iostream>
using namespace std;
class NodoLEG {
private:
int dato;

NodoLEG *siguiente;
public:
NodoLEG(int dato, NodoLEG *sig=NULL) {
this->dato = dato;
this->siguiente = sig;
}
};
class LEG {
private:
NodoLEG *list;
public:
LEG() {
list = NULL;
}
void insertar(int);
bool buscar(int);
};
void LEG::insertar(E x) {
list = new NodoLEG(x, list);
}
bool LEG::buscar(int x) {
NodoLEG *aux = list;
while (aux != NULL && aux->dato != x) {
aux = aux->siguiente;
}
if (aux == NULL) return false;
return true;
}
#endif

Anda mungkin juga menyukai