Bunun Burada Ne İşi Var?
Dün şehre inmek için Sayın Menderes Türel’in zamanında Hafif Metro ...
Dördüncü Veri Yapıları (Data Structures) projemiz çizgeler (graphs) üzerineydi. Proje temel olarak aşağıdakileri içeriyor:
Bu JAVA projesi Windows Vista üzerinde Eclipse Ganymede kullanılarak geliştirilmiştir. Kodlamada foreach ve generic tip tarzı yenilikler içerdiğinden sadece JAVA 5 ile çalışır. (JDK 1.6)
Metotların işleyişi, programın kullanımı ve benzer konularda daha ayrıntılı bilgiyi projenin raporunda bulabilirsiniz.
Ayrıca ödev kapsamında araştırma konusu olarak seçtiğimiz Huffman Decoding & Encoding hakkında Türkçe bir araştırmayı da ödev raporunda bulabilirsiniz. Araştırmanın kaynakları raporun sonunda belirtilmiştir.
Kaynak kodlarını örnek almak, fikir edinmek ve bilgi sahibi olma amaçlı kullanabilirsiniz. Eğer projenin kodlarını indirmeden incelemek isterseniz, yazının devamında kodları bulabilirsiniz. Sınıfların ne işe yaradığı gibi teknik bilgiler proje raporunda bulunmaktadır.
Tüm bilmuhçulara sınavda başarılar diliyorum.
Algorithms.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | import java.util.Vector; public class Algorithms { /** * @param: Ağırlıklı çizge * @param: Başlangıç düğüm numarası * @param: Bitiş düğüm numarası (-1: tüm düğümleri bul) * @return: En kısa yol sırasını içeren dizi * @see: http://www.cs.fit.edu/~ryan/java/programs/graph/Dijkstra-java.html * @author: UB */ /* Kaynak kodunda değişiklikler ve eklemeler yapılmıştır. */ public static int[] dijkstra(WeightedGraph G, int s, int f) { final int[] dist = new int[G.size()]; final int[] pred = new int[G.size()]; final boolean[] visited = new boolean[G.size()]; for (int i = 0; i < dist.length; i++) { dist[i] = Integer.MAX_VALUE; } dist[s] = 0; for (int i = 0; i < dist.length; i++) { final int next = minVertex(dist, visited); /* Çizge bağlı değilse yol bulunamaz. */ if (next == -1) { for (int a = 0; a < pred.length; a++) { pred[a] = -1; } return pred; } visited[next] = true; /* Eğer istediğimiz şehre ulaşmışsak devam etmemize gerek yok. */ if (next == f) return pred; final int[] n = G.neighbors(next); for (int j = 0; j < n.length; j++) { final int v = n[j]; final int d = dist[next] + G.getWeight(next, v); if (dist[v] > d) { dist[v] = d; pred[v] = next; } } } return pred; } /** * @param: Ağırlıklı çizge * @param: Başlangıç düğüm numarası * @return: MST bulunduktan sonra düğümler arası bağlantıları içeren dizi * @see: http://www.cs.fit.edu/~ryan/java/programs/graph/Prim-java.html * @author: Özlem */ public static int[] prim(WeightedGraph G, int s) { final int[] dist = new int[G.size()]; final int[] pred = new int[G.size()]; final boolean[] visited = new boolean[G.size()]; for (int i = 0; i < dist.length; i++) { dist[i] = Integer.MAX_VALUE; } dist[s] = 0; for (int i = 0; i < dist.length; i++) { final int next = minVertex(dist, visited); /* Yol bulunamazsa */ if (next == -1) { for (int a = 0; a < pred.length; a++) { pred[a] = -1; } return pred; } visited[next] = true; final int[] n = G.neighbors(next); for (int j = 0; j < n.length; j++) { final int v = n[j]; final int d = G.getWeight(next, v); if (dist[v] > d) { dist[v] = d; pred[v] = next; } } } return pred; } /** * @param: Uzaklık dizisi * @param: Gezilme durumu dizisi * @return: Gezilmeyen en yakın düğüm * @see http://www.cs.fit.edu/~ryan/java/programs/graph/Dijkstra-java.html */ private static int minVertex(int[] dist, boolean[] v) { int x = Integer.MAX_VALUE; int y = -1; for (int i = 0; i < dist.length; i++) { if (!v[i] && dist[i] < x) { y = i; x = dist[i]; } } return y; } /** * @param: Ağırlıklı çizge * @param: En kısa yol bilgilerini içeren dizi * @param: Başlangıç düğümü * @param: Bitiş düğümü * @return: Başlangıç ile bitiş arasındaki düğüm adları vektörü * @author: Özlem */ public static Vector<String> path(WeightedGraph G, int[] pred, int s, int e) { final Vector<String> path = new Vector<String>(); int x = e; while (x != s) { path.add(0, G.getLabel(x)); x = pred[x]; } path.add(0, G.getLabel(s)); return path; } /** * @param: Ağırlıklı çizge * @return: Çizgenin yönlü olup olmadığı * @author: Hüss */ public static boolean isDirected(WeightedGraph G) { for (int i = 0; i < G.size(); i++) { for (int x = 0; x < G.size(); x++) { if (G.getWeight(i, x) != G.getWeight(x, i)) { return true; } } } return false; } /** * @param: Ağırlıklı çizge * @param: Başlangıç düğüm numarası * @return: BFS ile gezilince oluşacak düğüm sırası * @author: Didem */ public static int[] bfs(WeightedGraph w, int startNode) { BListe kuyruk = new BListe(); boolean gezildi[] = new boolean[w.size()]; int sonuc[] = new int[w.size()]; for (int i = 1; i < w.size(); i++) { sonuc[i] = -1; /* Eğer çizge bağlı değilse, yollar -1 kalır. */ gezildi[i] = false; } kuyruk.ekle(startNode); gezildi[startNode] = true; int indis = 0; while (!kuyruk.bosMu()) { sonuc[indis] = kuyruk.cikar(); int b[] = w.neighbors(sonuc[indis]); for (int a = 0; a < b.length; a++) { if (!gezildi[b[a]]) { kuyruk.ekle(b[a]); } gezildi[b[a]] = true; } indis++; } return sonuc; } } |
BListe.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | /** BListe * @author: UB * * Kuyruk oluşturmak için kullanılan sınıftır. * Banka kuyruğu ödevinden hazır alınıp üzerinde * oynamalar yapılmıştır. */ public class BListe { private BListeNode bas; // Kuyruğun ilk elemanının referansı private BListeNode son; // Kuyruğun son elemanının referansı /* İlk yaratıldığında boş olan bir kuyruk */ public BListe() { bas = null; son = null; } public void ekle(int node) { BListeNode yeni = new BListeNode(node); if (son != null) { son.sonraki = yeni; son = yeni; } else { bas = yeni; son = yeni; } } public int cikar() { BListeNode dondurulecek = bas; if (bas != null) { bas = bas.sonraki; } if (bas == null) { son = null; } if(dondurulecek == null) { return -1; } return dondurulecek.node; } /* Kuyruğun bitip bitmediğini döndürür. */ public boolean bosMu() { return (bas == null ? true : false); } } |
BListeNode.java
1 2 3 4 5 6 7 8 9 | public class BListeNode { public int node; public BListeNode sonraki; public BListeNode (int node) { this.node = node; sonraki = null; } } |
Gorsel.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | import javax.swing.JPanel; import javax.swing.JFrame; import java.awt.Dimension; import java.awt.TextArea; import java.awt.Rectangle; import java.awt.Label; import java.awt.TextField; import javax.swing.JButton; import java.awt.Point; public class Gorsel extends JFrame { private static final long serialVersionUID = 1L; private JPanel jContentPane = null; private TextArea textMatris = null; private Label etiketMatris = null; private Label etiketDugumBilgiler = null; private TextField textDugumNo = null; private JButton butonGoster = null; private TextArea textDugumBilgi = null; private TextField textDij1 = null; private Label labeldij1 = null; private TextField textDij2 = null; private Label etiketDij2 = null; private JButton butonDij = null; private JButton butonBreFirst = null; private Label etiletBre2 = null; private TextField textBre = null; private JButton butonMST = null; private Label etkiketMST = null; private TextArea textResult = null; private JButton butonYenile = null; private Label etiketFile = null; private TextField textPrim = null; /** * This is the default constructor */ public Gorsel() { super(); initialize(); } /** * This method initializes this * * @return void */ private void initialize() { this.setSize(640, 510); this.setResizable(false); this.setContentPane(getJContentPane()); this.setTitle("Proje 4 [ÇBS]"); textMatris.setText(Main.maliyetDiziScreen()); } /** * This method initializes jContentPane * * @return javax.swing.JPanel */ private JPanel getJContentPane() { if (jContentPane == null) { etiketFile = new Label(); etiketFile.setBounds(new Rectangle(377, 461, 154, 15)); etiketFile.setText("Dosyaları tekrardan okuyun"); etkiketMST = new Label(); etkiketMST.setBounds(new Rectangle(237, 294, 294, 16)); etkiketMST.setText("şehrini kök düğüm alarak Prim algortiması ile MST'yi"); etiletBre2 = new Label(); etiletBre2.setBounds(new Rectangle(151, 265, 381, 18)); etiletBre2.setText("nolu/isimli şehirden başlayarak önce genişliğine (breath first) dolaş"); etiketDij2 = new Label(); etiketDij2.setBounds(new Rectangle(375, 242, 154, 15)); etiketDij2.setText("arasındaki en kısa yolu bul!"); labeldij1 = new Label(); labeldij1.setBounds(new Rectangle(151, 244, 111, 15)); labeldij1.setName("label2"); labeldij1.setText("nolu/isimli şehir ile"); etiketDugumBilgiler = new Label(); etiketDugumBilgiler.setBounds(new Rectangle(384, 11, 146, 15)); etiketDugumBilgiler.setText("no/isimli şehrin bilgilerini"); etiketMatris = new Label(); etiketMatris.setBounds(new Rectangle(10, 11, 290, 14)); etiketMatris.setText("Ağırlık matrisi: (0 bağlantısız anlamına gelir.)"); jContentPane = new JPanel(); jContentPane.setLayout(null); jContentPane.add(getTextMatris(), null); jContentPane.add(etiketMatris, null); jContentPane.add(etiketDugumBilgiler, null); jContentPane.add(getTextDugumNo(), null); jContentPane.add(getButonGoster(), null); jContentPane.add(getTextDugumBilgi(), null); jContentPane.add(getTextDij1(), null); jContentPane.add(labeldij1, null); jContentPane.add(getTextDij2(), null); jContentPane.add(etiketDij2, null); jContentPane.add(getButonDij(), null); jContentPane.add(getButonBreFirst(), null); jContentPane.add(etiletBre2, null); jContentPane.add(getTextBre(), null); jContentPane.add(getButonMST(), null); jContentPane.add(etkiketMST, null); jContentPane.add(getTextResult(), null); jContentPane.add(getButonYenile(), null); jContentPane.add(etiketFile, null); jContentPane.add(getTextPrim(), null); } return jContentPane; } /** * This method initializes textMatris * * @return java.awt.TextArea */ private TextArea getTextMatris() { if (textMatris == null) { textMatris = new TextArea(); textMatris.setBounds(new Rectangle(10, 29, 291, 200)); textMatris.setEditable(false); } return textMatris; } /** * This method initializes textDugumNo * * @return java.awt.TextField */ private TextField getTextDugumNo() { if (textDugumNo == null) { textDugumNo = new TextField(); textDugumNo.setBounds(new Rectangle(320, 9, 60, 18)); textDugumNo.setText("0"); } return textDugumNo; } /** * This method initializes butonGoster * * @return javax.swing.JButton */ private JButton getButonGoster() { if (butonGoster == null) { butonGoster = new JButton(); butonGoster.setBounds(new Rectangle(535, 7, 85, 20)); butonGoster.setText("göster"); butonGoster.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { if(Main.isParsableToInt(textDugumNo.getText())) { textDugumBilgi.setText(Main.getNodeDetails(Integer.parseInt(textDugumNo.getText()))); } else { if(Main.wg.labelToVertex(textDugumNo.getText()) != -1) { textDugumBilgi.setText(Main.getNodeDetails(Main.wg.labelToVertex(textDugumNo.getText()))); } else { textDugumBilgi.setText("Böyle bir şehir yok."); } } } }); } return butonGoster; } /** * This method initializes textDugumBilgi * * @return java.awt.TextArea */ private TextArea getTextDugumBilgi() { if (textDugumBilgi == null) { textDugumBilgi = new TextArea(); textDugumBilgi.setBounds(new Rectangle(320, 30, 301, 200)); textDugumBilgi.setEditable(false); } return textDugumBilgi; } /** * This method initializes textDij1 * * @return java.awt.TextField */ private TextField getTextDij1() { if (textDij1 == null) { textDij1 = new TextField(); textDij1.setBounds(new Rectangle(45, 238, 97, 22)); textDij1.setText("0"); } return textDij1; } /** * This method initializes textDij2 * * @return java.awt.TextField */ private TextField getTextDij2() { if (textDij2 == null) { textDij2 = new TextField(); textDij2.setBounds(new Rectangle(269, 239, 98, 20)); textDij2.setText("tüm şehirler"); } return textDij2; } /** * This method initializes butonDij * * @return javax.swing.JButton */ private JButton getButonDij() { if (butonDij == null) { butonDij = new JButton(); butonDij.setBounds(new Rectangle(536, 237, 85, 21)); butonDij.setText("Dijkstra!"); butonDij.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { int d1=-1,d2=-1; if(!Main.isParsableToInt(textDij1.getText())) { if(Main.wg.labelToVertex(textDij1.getText()) != -1) { d1 = Main.wg.labelToVertex(textDij1.getText()); } else { textResult.setText("Birinci şehir yok."); } } else { d1 = Integer.parseInt(textDij1.getText()); } if(!Main.isParsableToInt(textDij2.getText())) { if(Main.wg.labelToVertex(textDij2.getText()) != -1) { d2 = Main.wg.labelToVertex(textDij2.getText()); } else if (textDij2.getText().equalsIgnoreCase("tüm şehirler")) { textResult.setText(Main.dijkstra(d1,-1)); } else { textResult.setText("İkinci şehir yok."); } } else { d2 = Integer.parseInt(textDij2.getText()); } textResult.setText(Main.dijkstra(d1,d2)); } }); } return butonDij; } /** * This method initializes butonBreFirst * * @return javax.swing.JButton */ private JButton getButonBreFirst() { if (butonBreFirst == null) { butonBreFirst = new JButton(); butonBreFirst.setText("Dolaş"); butonBreFirst.setSize(new Dimension(85, 21)); butonBreFirst.setLocation(new Point(537, 264)); butonBreFirst.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { if(Main.isParsableToInt(textBre.getText())) { textResult.setText(Main.bfs(Integer.parseInt(textBre.getText()))); } else { if(Main.wg.labelToVertex(textBre.getText()) != -1) { textResult.setText(Main.bfs(Main.wg.labelToVertex(textBre.getText()))); } else { textResult.setText("Böyle bir şehir yok."); } } } }); } return butonBreFirst; } /** * This method initializes textBre * * @return java.awt.TextField */ private TextField getTextBre() { if (textBre == null) { textBre = new TextField(); textBre.setBounds(new Rectangle(45, 264, 98, 20)); textBre.setText("0"); } return textBre; } /** * This method initializes butonMST * * @return javax.swing.JButton */ private JButton getButonMST() { if (butonMST == null) { butonMST = new JButton(); butonMST.setText("Bul"); butonMST.setSize(new Dimension(85, 21)); butonMST.setLocation(new Point(537, 292)); butonMST.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { if(Main.isParsableToInt(textPrim.getText())) { textResult.setText("Kolay karşılaştırılması için sonuçlar sağ üst kutudadır."); textDugumBilgi.setText(Main.mst(Integer.parseInt(textPrim.getText()))); } else { textResult.setText("Kolay karşılaştırılması için sonuçlar sağ üst kutudadır."); textDugumBilgi.setText(Main.mst(Main.wg.labelToVertex(textPrim.getText()))); } } }); } return butonMST; } /** * This method initializes textResult * * @return java.awt.TextArea */ private TextArea getTextResult() { if (textResult == null) { textResult = new TextArea(); textResult.setBounds(new Rectangle(9, 321, 612, 126)); textResult.setEditable(false); textResult.setText("Sonuçlar burada görüntülenecek."); } return textResult; } /** * This method initializes butonYenile * * @return javax.swing.JButton */ private JButton getButonYenile() { if (butonYenile == null) { butonYenile = new JButton(); butonYenile.setText("Yenile"); butonYenile.setSize(new Dimension(85, 21)); butonYenile.setLocation(new Point(533, 454)); butonYenile.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent e) { Main.load(); textMatris.setText(Main.maliyetDiziScreen()); } }); } return butonYenile; } /** * This method initializes textPrim * * @return java.awt.TextField */ private TextField getTextPrim() { if (textPrim == null) { textPrim = new TextField(); textPrim.setBounds(new Rectangle(147, 290, 86, 21)); textPrim.setText("0"); } return textPrim; } } // @jve:decl-index=0:visual-constraint="10,10" |
Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 | import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Vector; public class Main { public static WeightedGraph wg; /** * Dosyadan veri okur. * * @author: UB * @link: http://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml */ public static void load() { /* * iller.txt okunması * * iller.txt illerin adını (ve belki de sayısını) tutan dosyadır. * Dosyanın aşağıdaki iki formattan birinde olması gerekir: * * FORMAT 1: 5 Antalya İzmir Manisa Ankara İstanbul Paris * * (beş şehir olması gerekmemektedir, 5 sayısal ifadesi görmezden * gelinecektir.) * * FORMAT 2: Antalya İzmir Manisa Ankara İstanbul Hakkari */ Vector<String> sehirAdlari = new Vector<String>(); try { FileInputStream fstream = new FileInputStream("iller.txt"); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; /* * İlk satırda şehrin adı yerine şehir sayısı verildiyse sisteme * ekleme. */ /* * Böylece hem şehir sayısı verilen hem verilmeyen dosyalar için * uyumluluk sağlanır. */ if ((strLine = br.readLine()) != null) { if (!isParsableToInt(strLine)) { sehirAdlari.add(strLine); } } while ((strLine = br.readLine()) != null) { sehirAdlari.add(strLine); } in.close(); } catch (Exception e) { System.out.println("iller.txt okunamadı."); } /* * maliyet.txt okunması * * maliyet.txt iller arasındaki maliyetleri tutan dosyadır. Eğer iller * arasında bağlantı yoksa -1 kullanılmalıdır. Dosyada ilsayısı^2 tane * sayısal (bu projede integer) değer olmalıdır. Daha az olduğu durumda * program kalan değerleri -1 olarak değerlendirecektir. Ay şekilde * dosya okunamadığında da hiçbir il arasında bağlantı olmadığı * düşünülecektir. Eğer ilsayısı^2'den fazla değer varsa sondaki * değerler ihmal edilecektir. Değerlerin satırlara doğru yerleşmesinin * ya da arada fazla boşluk olmasının bir anlamı yoktur. Aralarda * geçersiz değerler varsa (örnek 1 2 3 SFGSG 45) bu değerler -1 olarak * değerlendirilecektir. */ wg = new WeightedGraph(sehirAdlari.size()); for (int i = 0; i < sehirAdlari.size(); i++) { wg.setLabel(i, sehirAdlari.elementAt(i)); } String maliyetData = ""; try { FileInputStream fstream = new FileInputStream("maliyet.txt"); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String strLine; /* * Tüm dosya satır satır okunur. Böylece satır sonu karakterlerinden * (CR LF) kurtulunmuş olur. */ while ((strLine = br.readLine()) != null) { maliyetData = maliyetData + strLine + ' '; /* * Eğer sona boşluk eklenmezse alt satıra alt satır ile üst * satır birleşik kabul edilir. */ } in.close(); } catch (Exception e) { System.out.println("maliyet.txt okunamadı."); } StringTokenizer st = new StringTokenizer(maliyetData); for (int x = 0; x < sehirAdlari.size(); x++) { for (int y = 0; y < sehirAdlari.size(); y++) { String temp; if (st.hasMoreTokens()) { temp = st.nextToken(); if (isParsableToInt(temp) && Integer.parseInt(temp) > 0) { wg.addEdge(x, y, Integer.parseInt(temp)); } } } } } /** * @author: Didem */ public static void main(String[] args) { load(); Gorsel g = new Gorsel(); g.setVisible(true); } /** * Ekrana yazdırılmaya uygun bir dizaynda maliyet dizisinin içindeki * bilgileri gönderir. * * @author Didem * @return String */ public static String maliyetDiziScreen() { String cikti = ""; for (int i = 0; i < wg.size(); i++) { cikti = cikti + wg.getLabel(i) + "tt"; for (int x = 0; x < wg.size(); x++) { cikti = cikti + wg.getWeight(i, x) + "t"; } cikti = cikti + "n"; } return cikti; } /** * Düğümün içindeki bilgileri ekrana yazılmaya uygun formatta * gönderir. * @param: Düğüm no * @return: String * @author: Didem */ public static String getNodeDetails(int nid) { if (nid < 0 || nid >= wg.size()) { return "Geçersiz düğüm numarası."; } String cikti = ""; cikti = cikti + "Köşe numarası: " + nid + "n"; cikti = cikti + "Şehir adı: " + wg.getLabel(nid) + "n"; cikti = cikti + "Giden bağlantılar: (" + wg.neighbors(nid).length + ")n"; for (int a = 0; a < wg.neighbors(nid).length; a++) { cikti = cikti + "(" + wg.neighbors(nid)[a] + ") " + wg.getLabel(wg.neighbors(nid)[a]) + ": " + wg.getWeight(nid, wg.neighbors(nid)[a]) + "n"; } cikti = cikti + "Gelen bağlantılar: (" + wg.incoming(nid).length + ")n"; for (int a = 0; a < wg.incoming(nid).length; a++) { cikti = cikti + "(" + wg.incoming(nid)[a] + ") " + wg.getLabel(wg.incoming(nid)[a]) + ": " + wg.getWeight(wg.incoming(nid)[a], nid) + "n"; } return cikti; } /** * @param: Başlangıç düğüm no * @return: String * @author: Özlem */ public static String bfs(int start) { if (start < 0 || start >= wg.size()) { return "Geçersiz düğüm numarası."; } String cikti = ""; int sonuc[] = Algorithms.bfs(wg, start); for (int n = 0; n < sonuc.length; n++) { if (sonuc[n] == -1) { cikti = cikti + "Çizgenin devamına bağlantı bulunamadı."; break; } else { cikti = cikti + "[" + wg.getLabel(sonuc[n]) + "]n"; } } return cikti; } /** * @param: Başlangıç düğüm no * @return: String * @author: UB */ public static String mst(int start) { if (start < 0 || start >= wg.size()) { return "Geçersiz düğüm numarası."; } String cikti = ""; if (Algorithms.isDirected(wg)) { return "Prim algoritması sadece yönsüz algoritmalardançalışır. " + "Edmonds algoritması ne kadarnyapılmaya çalışıldıysa da,n" + "pek başarılı olduğumuznsöylenemez."; } int sonuc[] = Algorithms.prim(wg, start); if (sonuc[0] == -1) { return "Yol yok"; } for (int i = 0; i < wg.size(); i++) { cikti = cikti + wg.getLabel(i) + "tt"; for (int x = 0; x < wg.size(); x++) { cikti = cikti + ((sonuc[i] == x || sonuc[x] == i) && (i != x) ? wg .getWeight(i, x) : "0") + "t"; } cikti = cikti + "n"; } return cikti; } /** * @param: Başlangıç düğüm no * @param: Bitiş düğüm no (-1 hepsi) * @return: String * @author: Özlem */ public static String dijkstra(int start, int finish) { if (start < 0 || start >= wg.size()) { return "Geçersiz birinci düğüm numarası."; } if (finish < -1 || finish >= wg.size()) { return "Geçersiz ikinci düğüm numarası."; } String cikti = ""; Vector<String> Sehirler = new Vector<String>(); int yol[] = Algorithms.dijkstra(wg, start, finish); if (yol[0] == -1) { return "Yol yok"; } if (finish == -1) { for (int n = 0; n < wg.size(); n++) { if (n == start) { continue; } Sehirler = Algorithms.path(wg, yol, start, n); for (String x : Sehirler) { cikti = cikti + "[" + x + "] "; } cikti = cikti + "n"; } } else { Sehirler = Algorithms.path(wg, yol, start, finish); for (String x : Sehirler) { cikti = cikti + "[" + x + "] "; } cikti = cikti + "n"; } return cikti; } /** * @param: String bir değer * @return: Stringin bir tam sayı olup olmadığı */ public static boolean isParsableToInt(String in) { try { Integer.parseInt(in); return true; } catch (Exception e) { return false; } } } |
WeightedGraph.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | /** * @see: http://www.cs.fit.edu/~ryan/java/programs/graph/WeightedGraph-java.html */ public class WeightedGraph implements Cloneable { /* Kaynak kodunda değişiklikler ve eklemeler yapılmıştır. */ private int[][] edges; private String[] labels; /** * Referans değil, çizgenin tamamını bire bir kopyalamaya çalışan * bir metotdur. * @deprecated * @author: UB */ public Object clone() { Cloneable theClone = new WeightedGraph(this.edges.clone(), this.labels.clone()); return theClone; } public WeightedGraph(int n) { edges = new int[n][n]; labels = new String[n]; } public WeightedGraph(int[][] egdes, String[] labels) { this.edges = egdes; this.labels = labels; } public int size() { return labels.length; } public void setLabel(int vertex, String label) { labels[vertex] = label; } public String getLabel(int vertex) { return labels[vertex]; } public void addEdge(int source, int target, int w) { edges[source][target] = w; } public void removeEdge(int source, int target) { edges[source][target] = 0; } public int getWeight(int source, int target) { return edges[source][target]; } public int[] neighbors(int vertex) { int count = 0; for (int i = 0; i < edges[vertex].length; i++) { if (edges[vertex][i] > 0) count++; } final int[] answer = new int[count]; count = 0; for (int i = 0; i < edges[vertex].length; i++) { if (edges[vertex][i] > 0) answer[count++] = i; } return answer; } /** Bir köşeye gelen bağlantıları bulur. * @author Hüss * @param vertex number * @return incoming links */ public int[] incoming(int vertex) { int count = 0; for (int i=0;i<labels.length;i++) { if(edges[i][vertex] > 0) { count++; } } final int[] answer = new int[count]; count = 0; for (int i=0;i<labels.length;i++) { if(edges[i][vertex] > 0) { answer[count++] = i; } } return answer; } /**Etiketin köşe numrasını bulur. Bulamazsa -1 gönderir. * @author Hüss * @param vertex label * @return vertex number */ public int labelToVertex(String name) { for(int i=0;i<labels.length;i++) { if(labels[i].equalsIgnoreCase(name)) { return i; } } return -1; } } |
Twitler yükleniyor... 5 saniye sonra
Bıdı bıdı bıdı bıdı dıdı dıdı dudu dudu hıdı hıdı hödü hödü yüklüyoruz öhüm öhüm bıdı bıdı vs vs... 6 nanosaniye önce
Yüklenmenin geç olmasının sebebi ben değilim, Twitter API'sinin yavaş olması. Gudu gudu hıdı hödö büdü büdü... 25697 asır önce
Ha tabi bunları okumuşsan, bu sitenin çok gizli bir özelliğini bulmuşsun demektir. ;) Tebrikler. Bu "sürpiz yumurta"yı bulduğunu bana da haber verir misin? Tıkla! 6 dinazor önce
aynı ödevi bizimde proje 5 olarak verilmişti c yaptık gayet sıkıcı fakat bitirince güzel birşeyler ortaya çıkmış oluyor..
bizede verdi hoca Ayrık işlemsel Yapılar dersinde
bizde yapacaz bakalım
OpenGL kullanarak bişeler yapmayı planlıyoruz hayırlısı
Oldukca basarılı bır calısma,