2013-02-10 32 views
14

मैं जावा में हंगेरियन एल्गोरिदम लागू करने की कोशिश कर रहा हूं। मेरे पास एक एनएक्सएन लागत मैट्रिक्स है। मैं this चरण-दर-चरण मार्गदर्शिका का पालन कर रहा हूं। तो मेरे पास कवर मैट्रिक्स [एन] [एन] और 2 एरे कवर किए गए पंक्तियों और कवर किए गए कोलों को ट्रैक करने के लिए हैं - पंक्तिकॉवर [एन], पंक्ति कॉलम [एन] (1 मतलब कवर किया गया है, 0 का मतलब खुला है)हंगेरियन एल्गोरिदम: न्यूनतम पंक्तियों वाले 0 तत्वों को कैसे कवर किया जाए?

मैं 0 को कैसे कवर कर सकता हूं लाइनों की न्यूनतम संख्या के साथ? क्या कोई मुझे सही दिशा दिखा सकता है?

किसी भी मदद/सुझाव की सराहना की जाएगी।

उत्तर

6

चेक Wikipedia article (section Matrix Interpretation) में एल्गोरिथ्म के 3 कदम है, वे लाइनों की न्यूनतम राशि को कवर करने की गणना करने के लिए एक तरह से समझाने के सभी 0 के

अद्यतन: निम्नलिखित एक और तरीका है की न्यूनतम संख्या प्राप्त करने के लिए है लाइनों कि 0's कवर:

import java.util.ArrayList; 
import java.util.List; 

public class MinLines { 
    enum LineType { NONE, HORIZONTAL, VERTICAL } 

    private static class Line { 
     int lineIndex; 
     LineType rowType; 
     Line(int lineIndex, LineType rowType) { 
      this.lineIndex = lineIndex; 
      this.rowType = rowType; 
     }  
     LineType getLineType() { 
      return rowType; 
     } 

     int getLineIndex() { 
      return lineIndex; 
     } 
     boolean isHorizontal() { 
      return rowType == LineType.HORIZONTAL; 
     } 
    } 

    private static boolean isZero(int[] array) { 
     for (int e : array) { 
      if (e != 0) { 
       return false; 
      } 
     } 
     return true; 
    } 

    public static List<Line> getMinLines(int[][] matrix) { 
     if (matrix.length != matrix[0].length) { 
      throw new IllegalArgumentException("Matrix should be square!"); 
     } 

     final int SIZE = matrix.length; 
     int[] zerosPerRow = new int[SIZE]; 
     int[] zerosPerCol = new int[SIZE]; 

     // Count the number of 0's per row and the number of 0's per column   
     for (int i = 0; i < SIZE; i++) { 
      for (int j = 0; j < SIZE; j++) { 
       if (matrix[i][j] == 0) { 
        zerosPerRow[i]++; 
        zerosPerCol[j]++; 
       } 
      } 
     } 

     // There should be at must SIZE lines, 
     // initialize the list with an initial capacity of SIZE 
     List<Line> lines = new ArrayList<Line>(SIZE); 

     LineType lastInsertedLineType = LineType.NONE; 

     // While there are 0's to count in either rows or colums... 
     while (!isZero(zerosPerRow) && !isZero(zerosPerCol)) { 
      // Search the largest count of 0's in both arrays 
      int max = -1; 
      Line lineWithMostZeros = null; 
      for (int i = 0; i < SIZE; i++) { 
       // If exists another count of 0's equal to "max" but in this one has 
       // the same direction as the last added line, then replace it with this 
       // 
       // The heuristic "fixes" the problem reported by @JustinWyss-Gallifent and @hkrish 
       if (zerosPerRow[i] > max || (zerosPerRow[i] == max && lastInsertedLineType == LineType.HORIZONTAL)) { 
        lineWithMostZeros = new Line(i, LineType.HORIZONTAL); 
        max = zerosPerRow[i]; 
       } 
      } 

      for (int i = 0; i < SIZE; i++) { 
       // Same as above 
       if (zerosPerCol[i] > max || (zerosPerCol[i] == max && lastInsertedLineType == LineType.VERTICAL)) { 
        lineWithMostZeros = new Line(i, LineType.VERTICAL); 
        max = zerosPerCol[i]; 
       } 
      } 

      // Delete the 0 count from the line 
      if (lineWithMostZeros.isHorizontal()) { 
       zerosPerRow[lineWithMostZeros.getLineIndex()] = 0; 
      } else { 
       zerosPerCol[lineWithMostZeros.getLineIndex()] = 0; 
      } 

      // Once you've found the line (either horizontal or vertical) with the greater 0's count 
      // iterate over it's elements and substract the 0's from the other lines 
      // Example: 
      //       0's x col: 
      //   [ 0 1 2 3 ] -> 1 
      //   [ 0 2 0 1 ] -> 2 
      //   [ 0 4 3 5 ] -> 1 
      //   [ 0 0 0 7 ] -> 3 
      //    | | | | 
      //    v v v v 
      // 0's x row: {4} 1 2 0 

      //   [ X 1 2 3 ] -> 0 
      //   [ X 2 0 1 ] -> 1 
      //   [ X 4 3 5 ] -> 0 
      //   [ X 0 0 7 ] -> 2 
      //    | | | | 
      //    v v v v 
      //   {0} 1 2 0 

      int index = lineWithMostZeros.getLineIndex(); 
      if (lineWithMostZeros.isHorizontal()) { 
       for (int j = 0; j < SIZE; j++) { 
        if (matrix[index][j] == 0) { 
         zerosPerCol[j]--; 
        } 
       } 
      } else { 
       for (int j = 0; j < SIZE; j++) { 
        if (matrix[j][index] == 0) { 
         zerosPerRow[j]--; 
        } 
       }      
      } 

      // Add the line to the list of lines 
      lines.add(lineWithMostZeros); 
      lastInsertedLineType = lineWithMostZeros.getLineType(); 
     } 
     return lines; 
    } 

    public static void main(String... args) { 
     int[][] example1 = 
     { 
      {0, 1, 0, 0, 5}, 
      {1, 0, 3, 4, 5}, 
      {7, 0, 0, 4, 5}, 
      {9, 0, 3, 4, 5}, 
      {3, 0, 3, 4, 5} 
     }; 

     int[][] example2 = 
     { 
      {0, 0, 1, 0}, 
      {0, 1, 1, 0}, 
      {1, 1, 0, 0}, 
      {1, 0, 0, 0}, 
     }; 

     int[][] example3 = 
     { 
      {0, 0, 0, 0, 0, 0}, 
      {0, 0, 0, 1, 0, 0}, 
      {0, 0, 1, 1, 0, 0}, 
      {0, 1, 1, 0, 0, 0}, 
      {0, 1, 0, 0, 0, 0}, 
      {0, 0, 0, 0, 0, 0} 
     }; 

     List<int[][]> examples = new ArrayList<int[][]>(); 
     examples.add(example1); 
     examples.add(example2); 
     examples.add(example3); 

     for (int[][] example : examples) { 
      List<Line> minLines = getMinLines(example); 
      System.out.printf("Min num of lines for example matrix is: %d\n", minLines.size()); 
      printResult(example, minLines); 
      System.out.println(); 
     } 
    } 

    private static void printResult(int[][] matrix, List<Line> lines) { 
     if (matrix.length != matrix[0].length) { 
      throw new IllegalArgumentException("Matrix should be square!"); 
     } 

     final int SIZE = matrix.length; 
     System.out.println("Before:"); 
     for (int i = 0; i < SIZE; i++) { 
      for (int j = 0; j < SIZE; j++) { 
       System.out.printf("%d ", matrix[i][j]); 
      } 
      System.out.println(); 
     } 

     for (Line line : lines) { 
      for (int i = 0; i < SIZE; i++) { 
       int index = line.getLineIndex(); 
       if (line.isHorizontal()) { 
        matrix[index][i] = matrix[index][i] < 0 ? -3 : -1; 
       } else { 
        matrix[i][index] = matrix[i][index] < 0 ? -3 : -2; 
       } 
      } 
     } 

     System.out.println("\nAfter:"); 
     for (int i = 0; i < SIZE; i++) { 
      for (int j = 0; j < SIZE; j++) { 
       System.out.printf("%s ", matrix[i][j] == -1 ? "-" : (matrix[i][j] == -2 ? "|" : (matrix[i][j] == -3 ? "+" : Integer.toString(matrix[i][j])))); 
      } 
      System.out.println(); 
     } 
    } 
} 

महत्वपूर्ण हिस्सा, getMinLines तरीका है यह रिटर्न लाइनों कि मैट्रिक्स कवर 0's प्रविष्टियों के साथ एक List। उदाहरण के लिए प्रिंट matrices:

Min num of lines for example matrix is: 3 
Before: 
0 1 0 0 5 
1 0 3 4 5 
7 0 0 4 5 
9 0 3 4 5 
3 0 3 4 5 

After: 
- + - - - 
1 | 3 4 5 
- + - - - 
9 | 3 4 5 
3 | 3 4 5 

Min num of lines for example matrix is: 4 
Before: 
0 0 1 0 
0 1 1 0 
1 1 0 0 
1 0 0 0 

After: 
| | | | 
| | | | 
| | | | 
| | | | 

Min num of lines for example matrix is: 6 
Before: 
0 0 0 0 0 0 
0 0 0 1 0 0 
0 0 1 1 0 0 
0 1 1 0 0 0 
0 1 0 0 0 0 
0 0 0 0 0 0 

After: 
- - - - - - 
- - - - - - 
- - - - - - 
- - - - - - 
- - - - - - 
- - - - - -  

मैं इस आप को बढ़ावा, हंगरी एल्गोरिथ्म के बाकी कठिन नहीं होना चाहिए

+0

धन्यवाद एक बहुत! यह बहुत उपयोगी और बहुत स्पष्ट था। निश्चित रूप से मुझे एक बढ़ावा देता है। मैं इसकी प्रशंसा करता हूँ। –

+0

आपका स्वागत है! खुशी हुई यह मदद की! – higuaro

+2

उस लाइन को मानते हुए WostMostZeros केवल कुछ मनमानी रेखा देता है यह काम नहीं कर सकता है।उदाहरण के लिए मैट्रिक्स पर विचार करें: '0010 0110 1100 1000' आपका कोड पहले कॉलम 4 (चार शून्यों के साथ) का चयन करेगा, लेकिन फिर अगली पंक्ति जो इसे चुनती है वह पंक्ति 1 (दो शेष शून्य के साथ) पंक्ति 4 के बाद होती है (दो शेष शून्य के साथ)) और फिर पंक्ति 2 (एक शेष शून्य के साथ) पंक्ति 3 के बाद (एक शेष शून्य के साथ) कुल पांच लाइनें देते हैं। –

1

मैं जानता हूँ कि इस सवाल का पहले लंबे समय हल किया गया है लागू करने के लिए दे उम्मीद है, लेकिन मैं चरण 3 के लिए अपना कार्यान्वयन साझा करना चाहता हूं जहां न्यूनतम शून्यों को इस तरह से खींचा जाना चाहिए कि सभी शून्य शामिल हैं।

यहाँ कैसे इस चरण के लिए मेरी एल्गोरिथ्म काम करता है पर एक संक्षिप्त विवरण है:

  • लूप सभी कक्षों पर, सेल एक शून्य मूल्य होता है, हम और अपने पड़ोसियों यह द्वारा एक लाइन गुजर आकर्षित करने के लिए की जरूरत है,
  • यह जानने के लिए कि किस दिशा में रेखा खींची जानी चाहिए, मैंने maxVH() नामक एक विधि बनाई है जो क्षैतिज रूप से बनाम शून्यों को गिनती से गिनती है, और एक पूर्णांक देता है। यदि पूर्णांक सकारात्मक है, तो एक लंबवत रेखा खींचें, अन्यथा शून्य या नकारात्मक, क्षैतिज रेखा खींचें।
  • colorNeighbors() विधि लाइनों को आकर्षित करेगी और उन्हें भी गिना जाएगा। इसके अलावा, यह उन तत्वों पर 1 रखेगा जहां रेखा लंबवत रूप से गुजरती है। -1 उन तत्वों पर जहां रेखा क्षैतिज रूप से गुजरती है। 2 उन तत्वों पर जहां 2 अंतरण रेखाएं गुजरती हैं (क्षैतिज और लंबवत)।

उन 3 विधियों का लाभ यह है कि हम उन तत्वों को जानते हैं जो दो बार कवर होते हैं, हम जानते हैं कि कौन से तत्व शामिल हैं, और जो कवर नहीं हैं। इसके अलावा, रेखाओं को चित्रित करते समय, हम लाइन काउंटर की संख्या में वृद्धि करते हैं।

हंगेरी एल्गोरिथ्म + उदाहरण के संपूर्ण क्रियान्वयन के लिए: Github

कोड + विस्तृत टिप्पणियां चरण 3 के लिए:

/** 
    * Step 3.1 
    * Loop through all elements, and run colorNeighbors when the element visited is equal to zero 
    * */ 
    public void coverZeros(){ 
     numLines = 0; 
     lines = new int[values.length][values.length]; 

     for(int row=0; row<values.length;row++){ 
      for(int col=0; col<values.length;col++){ 
       if(values[row][col] == 0) 
        colorNeighbors(row, col, maxVH(row, col)); 
      } 
     } 
    } 

    /** 
    * Step 3.2 
    * Checks which direction (vertical,horizontal) contains more zeros, every time a zero is found vertically, we increment the result 
    * and every time a zero is found horizontally, we decrement the result. At the end, result will be negative, zero or positive 
    * @param row Row index for the target cell 
    * @param col Column index for the target cell 
    * @return Positive integer means that the line passing by indexes [row][col] should be vertical, Zero or Negative means that the line passing by indexes [row][col] should be horizontal 
    * */ 
    private int maxVH(int row, int col){ 
     int result = 0; 
     for(int i=0; i<values.length;i++){ 
      if(values[i][col] == 0) 
       result++; 
      if(values[row][i] == 0) 
       result--; 
     } 
     return result; 
    } 

    /** 
    * Step 3.3 
    * Color the neighbors of the cell at index [row][col]. To know which direction to draw the lines, we pass maxVH value. 
    * @param row Row index for the target cell 
    * @param col Column index for the target cell 
    * @param maxVH Value return by the maxVH method, positive means the line to draw passing by indexes [row][col] is vertical, negative or zero means the line to draw passing by indexes [row][col] is horizontal 
    * */ 
    private void colorNeighbors(int row, int col, int maxVH){ 
     if(lines[row][col] == 2) // if cell is colored twice before (intersection cell), don't color it again 
      return; 

     if(maxVH > 0 && lines[row][col] == 1) // if cell colored vertically and needs to be recolored vertically, don't color it again (Allowing this step, will color the same line (result won't change), but the num of line will be incremented (wrong value for the num of line drawn)) 
      return; 

     if(maxVH <= 0 && lines[row][col] == -1) // if cell colored horizontally and needs to be recolored horizontally, don't color it again (Allowing this step, will color the same line (result won't change), but the num of line will be incremented (wrong value for the num of line drawn)) 
      return; 

     for(int i=0; i<values.length;i++){ // Loop on cell at indexes [row][col] and its neighbors 
      if(maxVH > 0) // if value of maxVH is positive, color vertically 
       lines[i][col] = lines[i][col] == -1 || lines[i][col] == 2 ? 2 : 1; // if cell was colored before as horizontal (-1), and now needs to be colored vertical (1), so this cell is an intersection (2). Else if this value was not colored before, color it vertically 
      else   // if value of maxVH is zero or negative color horizontally 
       lines[row][i] = lines[row][i] == 1 || lines[row][i] == 2 ? 2 : -1; // if cell was colored before as vertical (1), and now needs to be colored horizontal (-1), so this cell is an intersection (2). Else if this value was not colored before, color it horizontally 
     } 

     // increment line number 
     numLines++; 
//  printMatrix(lines); // Monitor the line draw steps 
    }//End step 3 
+1

का उत्तर देने के लिए खेद है, यह कार्यान्वयन भी काम नहीं करता है सोम के मामले इस चलाने की कोशिश करें: \t \t पूर्णांक [] [] मूल्यों = { \t \t {17, 10, 13, 2, 12, 11, 0}, \t \t {5, 8, 9, 0, 3, 5, 5 }, \t \t {0, 2, 0, 6, 9, 8, 13}, \t \t {26, 1, 11, 12, 0, 0, 13}, \t \t {17, 0, 20, 17, 25, 25, 23}, \t \t {0, 0, 4, 0, 10, 11, 0}, \t \t {12, 11, 0, 20, 12, 6, 14} \t \t}; – LocalHorst