Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.3 kB
2
Indexable
Never
public void exercice2(){
		  if(this.type == 1){
			  ex2_result = 0;
			  //Using DFS
				ex2_DFSNumInit();
				System.out.println("ex : final weight = "+ ex2_result);
		  } else {
			  System.out.println("Can't run ex2 on an undirected graph");
		  }
	}

	public int ex2_result;

	public void ex2_DFSNumInit(){
		nb = 0;
		// Checking for weighted or unweghted graph
		if(this.weighted == 1) {
			//INITIALISATION
			d = new int[this.adjlistW.length];
			f = new int[this.adjlistW.length];
			//Visiting the whole graph
			for (int i = 0; i < this.adjlistW.length; i++) {
				d[i] = 0;
				f[i] = 0;
			}

			for (int s = 0; s < this.adjlistW.length; s++) {
				if (d[s] == 0) ex2_DFSNumRecursiveW(s);
			}
		}
	}

	private void ex2_DFSNumRecursiveW(int s){
		nb++;
		d[s] = nb;

		//For each successor of s do...
		WeightedNode4A node = this.adjlistW[s];
		int successor=0;

		while(node != null){
			successor = node.getVal();

			if(d[successor] == 0) ex2_DFSNumRecursiveW(successor);
			if(node.getVal()%2 == 0){
				System.out.println("ex2 : weight "+ node.getVal()+" of arc " + s + " --> " + successor);
				ex2_result+= node.getVal();
			}
			node = node.getNext();
		}

		nb++;
		f[s] = nb;
	}