import java.util.Stack;
public class RecursiveDFS {
public static int graph[][] = {
{ 1, 2 },// 0
{ 0, 3, 4 },// 1
{ 0, 5, 6 },// 2
{ 1, 7 },// 3
{ 1, 7 },// 4
{ 2, 7 },// 5
{ 2, 7 },// 6
{ 3, 4, 5, 6 } // 7
};
static int visited[] = { -1, -1, -1, -1, -1, -1, -1, -1 };// Initialize all
// unvisited.
public static int[] findNeighbours(int v) {
return graph[v];
}
public static boolean isVisited(int v) {
if (visited[v] == 1)
return true;
else
return false;
}
public static void recursiveDFS(int vertex) {
System.out.print(vertex);
visited[vertex] = 1;
int neighbours[] = findNeighbours(vertex);
for (int i = 0; i < neighbours.length; i++) {
if (!isVisited(neighbours[i])) {
recursiveDFS(neighbours[i]);
}
}
}
public static void main(String[] args) throws InterruptedException {
recursiveDFS(0);
}
}
網頁
BloggerAds 廣告
標籤
- Java (96)
- Android (27)
- 演算法 (21)
- c++ (19)
- JavaScript (7)
- OpenMp (6)
- Design Pattern (4)
- 日文歌曲 (4)
- 資料結構 (4)
- Foundation Knowledge Of Programming (3)
- QUT (2)
- CodingHomeWork (1)
- Database (1)
- 英文歌詞 (1)
搜尋此網誌
2012年5月21日 星期一
Directed Graph Traversal using recursive DFS
DFS(Depth First Search) using a stack with adjacency list
import java.util.Stack; import sun.misc.Queue; public class DFS { public static int graph [][] = { {1,2},//0 {0,3,4},//1 {0,5,6},//2 {1,7},//3 {1,7},//4 {2,7},//5 {2,7},//6 {3,4,5,6}//7 }; static Stack stack = new Stack(); static Queue outPut = new Queue(); static int visited [] = {-1,-1,-1,-1,-1,-1,-1,-1};//Initialize all unvisited. public static int [] findNeighbours(int v){ return graph[v]; } public static boolean isVisited(int v){ if(visited[v]==1){ return true; }else{ return false; } } public static void main(String[] args) throws InterruptedException{ stack.push(0); while(!stack.empty()){ int vertex = (Integer)stack.pop(); if(!isVisited(vertex)){ visited[vertex]=1; outPut.enqueue(vertex); int neighbours []= findNeighbours(vertex); for(int i = 0 ;i<neighbours.length;i++){ if(!isVisited(neighbours[i])){ stack.push(neighbours[i]); } } } } while(!outPut.isEmpty()){ System.out.print(outPut.dequeue()); } } }
BFS
Android preference demo (Android 偏好設定)
Shared Preferences是用來儲存應用程式設定值的好用類別.
資料會一直存活在系統,直到應用程式被移除.
透過access preference name, 可以對資料做接收或者是寫入的動作.
範例如下.
========================
AndroidManifest.xml
資料會一直存活在系統,直到應用程式被移除.
透過access preference name, 可以對資料做接收或者是寫入的動作.
範例如下.
========================
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cookbook.activity_lifecycle"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity android:name=".PreferenceDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
PreferenceDemo.java
import com.cookbook.activity_lifecycle.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class PreferenceDemo extends Activity{
public static final String PREFS_NAME = "ap_settings";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pref) ;
final Button setbutton = (Button) findViewById(R.id.setButton);
setbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText et = (EditText)findViewById(R.id.editText1);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("edit_text", et.getText().toString());
// Commit the edits!
editor.commit();
et.setText("");
}
});
final Button getButton = (Button) findViewById(R.id.getButton);
getButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText et = (EditText)findViewById(R.id.editText1);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
et.setText(settings.getString("edit_text", null));
}
});
}
}
pref.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:text="null"/>
<Button
android:id="@+id/setButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Set" />
<Button
android:id="@+id/getButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Get" />
</LinearLayout>
Reference:Android Data Storage
訂閱:
文章 (Atom)
我的網誌清單
標籤
日文歌曲
(4)
股市
(7)
股票
(9)
英文歌詞
(1)
時事
(1)
硬體(hardware)
(1)
資料結構
(4)
演算法
(21)
數學(Math)
(4)
ACM
(3)
ajax
(7)
algorithms
(1)
Android
(27)
Blog Notes(部落格記事)
(6)
C
(9)
c++
(19)
CodingHomeWork
(1)
Database
(1)
Design Pattern
(4)
Foundation Knowledge Of Programming
(3)
GWT
(1)
How
(2)
J2EE
(1)
Java
(96)
Java語言
(4)
JavaScript
(7)
Leetcode
(4)
LOL
(1)
OpenMp
(6)
QUT
(2)
Uva
(2)
Yahoo知識問答
(11)