ผลต่างระหว่างรุ่นของ "01204212/friends"

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา
(หน้าที่ถูกสร้างด้วย ': ''This part of 01204212'' == Code == <syntaxhighlight lang="java"> import java.io.BufferedReader; import java.io.InputStreamRead...')
 
 
(ไม่แสดง 6 รุ่นระหว่างกลางโดยผู้ใช้คนเดียวกัน)
แถว 1: แถว 1:
 
: ''This part of [[01204212]]''
 
: ''This part of [[01204212]]''
 +
 +
In this task you will work with graphs from real social networks.
 +
 +
* [http://theory.cpe.ku.ac.th/~jittat/courses/01204212/tasks/graph1/facebook.in facebook.in] -- This is a graph from facebook.  It is an undirected graph.  The graph is from [https://snap.stanford.edu/data/egonets-Facebook.html].  It has 4,039 vertices and 88,234 edges.
 +
* [http://theory.cpe.ku.ac.th/~jittat/courses/01204212/tasks/graph1/twitter.in twitter.in] -- This is a graph from twitter.  It is a directed graph.  This graph is from [https://snap.stanford.edu/data/egonets-Twitter.html]. It has 81,306 vertices and 1,768,149 edges.
 +
 +
'''The graph format:'''
 +
 +
* First line: two integers ''n'' and ''m'', where ''n'' is the number of vertices and ''m'' is the number of edges
 +
* Next ''m'' lines: each line contains two integers ''u'' and ''v'' (ranging from 0 to n-1).  In an undirected graph, this means that there is an edge between ''u'' and ''v''.  In a directed graph, this means that there is an edge from ''u'' to ''v''.
 +
 +
== Tasks 1 ==
 +
For an undirected graph, find the vertices with the highest degree.  For a directed graph (twitter), find vertices with the highest in-degree and vertices with highest out-degree.
 +
 +
== Tasks 2 ==
 +
Consider vertex 0 and vertex ''n''-1.  Find the length of the shortest path from vertex 0 to vertex ''n''-1.
 +
 +
== Tasks 3 ==
 +
In twitter graph, find out how many vertices that are reachable from vertex 0 but vertex 0 cannot be reachable from them.
  
 
== Code ==
 
== Code ==
แถว 10: แถว 29:
 
public class Main {
 
public class Main {
  
public static void main(String[] args) {
+
    public static void main(String[] args) {
Main main = new Main();
+
        Main main = new Main();
+
       
main.process();
+
        main.process();
}
+
    }
+
   
int n,m;
+
    int n,m;
List<Integer> [] adjList;
+
    List<Integer> [] adjList;
+
   
void process() {
+
    void process() {
readInput();
+
        readInput();
}
+
    }
 +
 
 +
    private void readInput() {
 +
        BufferedReader reader = new BufferedReader(
 +
                  new InputStreamReader(System.in) );
 +
 
 +
        try {
 +
            String[] items = reader.readLine().split(" ");
  
private void readInput() {
+
            n = Integer.parseInt(items[0]);
    BufferedReader reader = new BufferedReader(
+
            m = Integer.parseInt(items[1]);
              new InputStreamReader(System.in) );
 
  
    try {
+
            adjList = (List<Integer>[])(new List[n]);
    String[] items = reader.readLine().split(" ");
+
            for(int i=0; i<n; i++) {
 +
                adjList[i] = new ArrayList<Integer>();
 +
            }
 +
           
 +
            for(int i=0; i<m; i++) {
 +
                items = reader.readLine().split(" ");
 +
                int u = Integer.parseInt(items[0]);
 +
                int v = Integer.parseInt(items[1]);
 +
               
 +
                adjList[u].add(v);
 +
                adjList[v].add(u);
 +
            }
 +
        } catch(Exception e) {
 +
        }
 +
    }
 +
}
 +
</syntaxhighlight>
  
    n = Integer.parseInt(items[0]);
+
=== findLevels ===
    m = Integer.parseInt(items[1]);
+
<syntaxhighlight lang="java">
 +
void findLevels(int s) {
 +
List<Integer> nextLevel = new ArrayList<Integer>();
 +
levels = new int[n];
 +
for(int u=0; u<n; u++) {
 +
levels[u] = -1;
 +
}
 +
levels[s] = 0;
 +
nextLevel.add(s);
 +
 +
while(! nextLevel.isEmpty()) {
 +
List<Integer> currentLevel = nextLevel;
 +
nextLevel = new ArrayList<Integer>();
  
    adjList = (List<Integer>[])(new List[n]);
+
for(int u : currentLevel) {
    for(int i=0; i<n; i++) {
+
for(int v : adjList[u]) {
    adjList[i] = new ArrayList<Integer>();
+
if(levels[v] == -1) {
    }
+
levels[v] = levels[u] + 1;
   
+
nextLevel.add(v);
    for(int i=0; i<m; i++) {
+
}
    items = reader.readLine().split(" ");
+
}
    int u = Integer.parseInt(items[0]) - 1;
+
}
    int v = Integer.parseInt(items[1]) - 1;
+
}
   
 
    adjList[u].add(v);
 
    adjList[v].add(u);
 
    }
 
    } catch(Exception e) {
 
    }
 
 
}
 
}
}
 
 
</syntaxhighlight>
 
</syntaxhighlight>

รุ่นแก้ไขปัจจุบันเมื่อ 06:40, 1 ธันวาคม 2559

This part of 01204212

In this task you will work with graphs from real social networks.

  • facebook.in -- This is a graph from facebook. It is an undirected graph. The graph is from [1]. It has 4,039 vertices and 88,234 edges.
  • twitter.in -- This is a graph from twitter. It is a directed graph. This graph is from [2]. It has 81,306 vertices and 1,768,149 edges.

The graph format:

  • First line: two integers n and m, where n is the number of vertices and m is the number of edges
  • Next m lines: each line contains two integers u and v (ranging from 0 to n-1). In an undirected graph, this means that there is an edge between u and v. In a directed graph, this means that there is an edge from u to v.

Tasks 1

For an undirected graph, find the vertices with the highest degree. For a directed graph (twitter), find vertices with the highest in-degree and vertices with highest out-degree.

Tasks 2

Consider vertex 0 and vertex n-1. Find the length of the shortest path from vertex 0 to vertex n-1.

Tasks 3

In twitter graph, find out how many vertices that are reachable from vertex 0 but vertex 0 cannot be reachable from them.

Code

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        Main main = new Main();
        
        main.process();
    }
    
    int n,m;
    List<Integer> [] adjList;
    
    void process() {
        readInput();
    }

    private void readInput() {
        BufferedReader reader = new BufferedReader(
                   new InputStreamReader(System.in) );

        try {
            String[] items = reader.readLine().split(" ");

            n = Integer.parseInt(items[0]);
            m = Integer.parseInt(items[1]);

            adjList = (List<Integer>[])(new List[n]);
            for(int i=0; i<n; i++) {
                adjList[i] = new ArrayList<Integer>();
            }
            
            for(int i=0; i<m; i++) {
                items = reader.readLine().split(" ");
                int u = Integer.parseInt(items[0]);
                int v = Integer.parseInt(items[1]);
                
                adjList[u].add(v);
                adjList[v].add(u);
            }
        } catch(Exception e) {
        }
    }
}

findLevels

	void findLevels(int s) {
		List<Integer> nextLevel = new ArrayList<Integer>();
		levels = new int[n];
		for(int u=0; u<n; u++) {
			levels[u] = -1;
		}
		levels[s] = 0;
		nextLevel.add(s);
		
		while(! nextLevel.isEmpty()) {
			List<Integer> currentLevel = nextLevel;
			nextLevel = new ArrayList<Integer>();

			for(int u : currentLevel) {
				for(int v : adjList[u]) {
					if(levels[v] == -1) {
						levels[v] = levels[u] + 1;
						nextLevel.add(v);
					}
				}
			}
		}
	}