补第四周作业

本文介绍了一种通过邻接矩阵表示图数据结构的方法,并提供了完整的Java实现代码。该方法首先从文件中读取图的基本信息,包括顶点数和边数,然后构建并输出图的邻接矩阵。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.weiwei;
import java.io.BufferedReader;  
import java.io.File;  
import java.io.FileOutputStream;  
import java.io.FileReader;  
import java.io.IOException;  
	import java.util.ArrayList;  
	  
	public class GraphRepresentation {  
	    
	    public static void main(String[] args) {  
	        //  自动生成的方法存根  
	        String path = "txt/tinyG.txt";  
	        ArrayList<Integer> list = read(path);  
	        // 调用产生邻接矩阵的函数  
	        int[][] arc = MGraph(list);  
	        // 控制台打印邻接矩阵  
	        for (int i = 0; i < arc.length; i++) {  
	            for (int j = 0; j < arc[0].length; j++) {  
	                System.out.print(arc[i][j]);  
	            }  
	            System.out.println("");  
	        }  
	        //写入tinyG_matrix.txt  
	        write(arc);  
	    }  
	  
	    // 构造图的邻接矩阵函数  
	    public static int[][] MGraph(ArrayList<Integer> list) {  
	  
	        // list数组中的前两个数据分别是图的顶点数目和边的数目  
	        int v = list.get(0);  
	        int e = list.get(1);  
	        // 创建一个二维数组arc来存储邻接矩阵  
	        int arc[][] = new int[v][e];  
	        // 初始化邻接矩阵  
	        for (int i = 0; i < v; i++) {  
	            for (int j = 0; j < v; j++) {  
	                arc[i][j] = 0;  
	            }  
	        }  
	        //给邻接矩阵赋值,1表示边存在关系  
	        for (int k = 0; k < e; k++) {  
	            for (int q = 2; q < list.size() - 2; q = q + 2) {  
	                arc[list.get(q)][list.get(q + 1)] = 1;  
	                arc[list.get(q + 1)][list.get(q)] = 1;  
	            }  
	        }  
	        return arc;  
	  
	    }  
	  
	    // 写入tinyG_matrix.txt  
	    public static void write(int[][] arc) {  
	        File f = new File("txt/tinyG_matrix.txt");  
	        FileOutputStream fou = null;  
	        String a="",b="";  
	        try {  
	  
	            fou = new FileOutputStream(f, false);// true,设置可追加  
	            for (int i = 0; i < arc.length; i++) {  
	                for (int j = 0; j < arc[0].length; j++) {  
	                    a =a+String.valueOf(arc[i][j]);  
	                }  
	                a=a+"\t\n";  
	            }  
	            fou.write(a.getBytes());  
	              
	        } catch (Exception e) {  
	            // TODO: handle exception  
	            e.printStackTrace();  
	        } finally {  
	            try {  
	                fou.close();  
	            } catch (Exception e) {  
	                // TODO Auto-generated catch block  
	                e.printStackTrace();  
	            }  
	        }  
	    }  
	  
	    // 读取文件到Arraylist 数组  
	    public static ArrayList read(String path) {  
	        ArrayList<Integer> list = new ArrayList<Integer>();  
	        BufferedReader input = null;  
	        try {  
	            FileReader in = new FileReader(path);  
	            input = new BufferedReader(in);  
	            String ss;  
	            try {  
	                while ((ss = input.readLine()) != null) {  
	                    String[] s = (ss.split(" "));  
	                    for (int i = 0; i < s.length; i++) {  
	                        list.add(Integer.parseInt(s[i].trim())); // 将String  
	                                                                    // s中的内容添加到动态数组中  
	                    }  
	  
	                }  
	            } catch (IOException e) {  
	                // 自动生成的 catch 块  
	                e.printStackTrace();  
	            }  
	            in.close();  
	            input.close();  
	        } catch (Exception e) {  
	            //  自动生成的 catch 块  
	            e.printStackTrace();  
	        }  
	  
	        return list;  
	    }  
	  
	}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值